keras图层没有属性密集

时间:2019-05-06 02:33:55

标签: python tensorflow keras

我目前正参与TensorFlow的Coursera简介,以进行人工智能,机器学习和深度学习课程。在以下代码中出现错误。

这是我的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

2 个答案:

答案 0 :(得分:0)

在TF 2.0.0-alpha0中尝试

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]))

答案 1 :(得分:0)

图层名称是Den s e,而不是Den c e。