繁体   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