系统信息
- 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
最佳答案
这应该没问题:
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.Sequential
和keras.layers.Dense
的用法。
关于python - 类型错误 : 'module' object is not callable.,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57562496/