簡體   English   中英

keras 層沒有屬性 Dense

[英]keras layers has no attribute Dense

我目前正在參與 Coursera-Introduction to TensorFlow for人工智能、機器學習和深度學習課程。 我在以下代碼中遇到錯誤。

這是我的python代碼,

# y = 2x - 1

import tensorflow as tf
# helps us to represent our data as lists easily and quickly
import numpy as np
# framework for defining a neural network as a set of Sequential layers
from tensorflow import keras

# The LOSS function measures the guessed answers against the known correct 
# answers and measures how well or how badly it did
# then uses the OPTIMIZER function to make another guess. Based on how the 
# loss function went, it will try to minimize the loss.

model = tf.keras.Sequential([keras.layers.Dence(units=1, input_shape= 
[1])])
model.compile(optimizer='sgd', loss='mean_squared_error')

# providing data
xs = np.array([-1.0,0.0,1.0,2.0,3.0,4.0],dtype=float)
ys = np.array([-3.0,-1.0,1.0,3.0,5.0,7.0],dtype=float)

# training neural network
model.fit(xs,ys,epochs=500)

# figure out value for unknown x
print(model.predict([10.0]))

我在終端中收到此錯誤消息。

C:\anaconda\envs\tfp\pythonw.exe C:/Users/USER/PycharmProjects/couseraTensorflow/helloWorld.py
Traceback (most recent call last):
  File "C:/Users/USER/PycharmProjects/couseraTensorflow/helloWorld.py", line 11, in <module>
    model = tf.keras.Sequential([keras.layers.Dence(units=1, input_shape=[1])])
AttributeError: module 'tensorflow._api.v1.keras.layers' has no attribute 'Dence'

Process finished with exit code 1

圖層名稱是 Den s e,而不是 Den c e。

在 TF 2.x 中試試這個

import tensorflow as tf
# helps us to represent our data as lists easily and quickly
import numpy as np
# framework for defining a neural network as a set of Sequential layers
from tensorflow import keras

# The LOSS function measures the guessed answers against the known correct 
# answers and measures how well or how badly it did
# then uses the OPTIMIZER function to make another guess. Based on how the 
# loss function went, it will try to minimize the loss.

model = tf.keras.models.Sequential([keras.layers.Dense(units=1, input_shape= 
[1])])
model.compile(optimizer='sgd', loss='mean_squared_error')

# providing data
xs = np.array([-1.0,0.0,1.0,2.0,3.0,4.0],dtype=float)
ys = np.array([-3.0,-1.0,1.0,3.0,5.0,7.0],dtype=float)

# training neural network
model.fit(xs,ys,epochs=500)

# figure out value for unknown x
print(model.predict([10.0]))

暫無
暫無

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

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