系统信息
-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
答案 0 :(得分:2)
这应该很好:
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
的用法。