我正在创建一个基本的 Tensorflow 深度学习模型。 我的代码是:-
# Creating the data
x = np.array(\[-7.0,-4.0,-1.0,2.0,5.0,8.0,11.0,14.0])
y = np.array(\[3.0,6.0,9.0,12.0,15.0,18.0,21.0,24.0])
## Turning the data into tensors
x = tf.cast(tf.constant(x ), dtype = tf.float32)
y = tf.cast(tf.constant(y ), dtype = tf.float32)
x = tf.expand_dims(x, axis = 0)
y = tf.expand_dims(y, axis = 0)
我的型号是:-
# Set the random seed
tf.random.set_seed(42)
# Create a model
model = tf.keras.Sequential([
tf.keras.layers.Dense(1)
])
# Compile the model
model.compile(loss = tf.keras.losses.mae,
optimizer = tf.keras.optimizers.SGD(),
metrics = ['accuracy']
)
# Fitting the model
model.fit(x,y, epochs = 5)
在使用模型进行预测时,我希望模型能够进行预测,但它给出了错误。
我的预测代码是:-
model. Predict([17.0])
错误是:-
TypeError Traceback (most recent call last)
<ipython-input-28-914f309df50b> in <cell line: 1>()
----> 1 model.predict([17.0], dtype = tf.float32)
1 frames
/usr/local/lib/python3.10/dist-packages/keras/utils/traceback_utils.py in error_handler(*args, **kwargs)
63 filtered_tb = None
64 try:
---> 65 return fn(*args, **kwargs)
66 except Exception as e:
67 filtered_tb = _process_traceback_frames(e.__traceback__)
TypeError: Model.predict() got an unexpected keyword argument 'dtype'
在 Tensorflow Keras API 中预测模型时,不接受 dtype argumnet。因此,您需要将输入数据作为 Numpy 数组传递。请找到下面的代码,我已经尝试过这个代码。我们不会收到任何错误。
# Creating the data
x = np.array(\[-7.0,-4.0,-1.0,2.0,5.0,8.0,11.0,14.0])
y = np.array(\[3.0,6.0,9.0,12.0,15.0,18.0,21.0,24.0])
## Turning the data into tensors
x = tf.cast(tf.constant(x ), dtype = tf.float32)
y = tf.cast(tf.constant(y ), dtype = tf.float32)
x = tf.expand_dims(x, axis = 0)
y = tf.expand_dims(y, axis = 0)
# Set the random seed
tf.random.set_seed(42)
# Create a model
model = tf.keras.Sequential([
tf.keras.layers.Dense(1)
])
# Compile the model
model.compile(loss = tf.keras.losses.mae,
optimizer = tf.keras.optimizers.SGD(),
metrics = ['accuracy'])
# Fitting the model
model.fit(x,y, epochs = 5)
# Model Predict
prediction = model.predict(np.array([17.0]))
print(prediction)