【问题标题】:TypeError: 'module' object is not callable. kerasTypeError:“模块”对象不可调用。喀拉斯
【发布时间】:2019-08-19 18:56:48
【问题描述】:

系统信息
- Windows 10
- TensorFlow 后端(是/否):是
- TensorFlow 版本:1.14.0
- Keras 版本:2.24
- Python 版本:3.6
- CUDA/cuDNN 版本:10
- GPU 型号和内存:gtx 1050 ti

描述当前行为
我通过 conda 安装了 tensoflow 和 keras。然后我尝试运行这段代码:

import tensorflow as tf
import keras
import numpy as np

model = keras.Sequential([keras.layers(units=1, input_shape=[1])])

model.compile(optimizer="sgd", loss="mean_squared_error")

x = np.array([-1, 0, 1, 2, 3, 4])
y = np.array([-3, -1, 1, 3, 5, 7])

model.fit(x, y, epochs=500)

print(model.predict([10]))`

当我运行这段代码时,我得到了错误:

Using TensorFlow backend.
Traceback (most recent call last):
  File "C:/Users/xxx/PycharmProjects/Workspace/tensorflow/hello_world_of_nn.py", line 5, in <module>
    model = keras.Sequential([keras.layers(units=1, input_shape=[1])])
TypeError: 'module' object is not callable

当我尝试这个时:
python -c 'import keras as k; print(k.__version__)'

我得到错误:

C:\Users\xxx>python -c 'import keras as k; print(k.__version__)'
  File "<string>", line 1
    'import
          ^
SyntaxError: EOL while scanning string literal

【问题讨论】:

    标签: python tensorflow keras anaconda conda


    【解决方案1】:

    这应该没问题:

    import tensorflow as tf
    import keras
    import numpy as np
    
    model = keras.models.Sequential([keras.layers.Dense(units=1, input_shape=[1])])
    
    model.compile(optimizer="sgd", loss="mean_squared_error")
    
    x = np.array([-1, 0, 1, 2, 3, 4])
    y = np.array([-3, -1, 1, 3, 5, 7])
    
    model.fit(x, y, epochs=500)
    
    print(model.predict([10]))
    

    请注意keras.models.Sequentialkeras.layers.Dense的用法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-17
      • 2014-09-21
      • 2011-05-30
      • 2021-11-22
      • 2017-05-08
      相关资源
      最近更新 更多