skip to Main Content

I’m currently involving Coursera-Introduction to TensorFlow for Artificial Intelligence, Machine Learning, and Deep Learning course. I got an error in the following code.

Here is my python code,

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

I got this error message in terminal.

C:anacondaenvstfppythonw.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

Answers


  1. try this in 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]))
    
    Login or Signup to reply.
  2. The layername is Dense, not Dence.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search