1

系统信息
- 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
4

1 回答 1

3

这应该没问题:

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

于 2019-08-19T19:03:54.227 回答