簡體   English   中英

TypeError:“模塊”對象不可調用。 凱拉斯

[英]TypeError: 'module' object is not callable. keras

系統信息
-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.Sequentialkeras.layers.Dense的用法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM