此问题在此处已有答案:
What function defines accuracy in Keras when the loss is mean squared error (MSE)?(3个答案)
上个月关门了。
我尝试学习一个有角的网络,学习y=x**2,但精度为0;我通过阅读文档来编写此代码。
import numpy as np
import tensorflow as tf
from tensorflow import keras
from keras import layers
inputs=keras.Input(shape=(1,))
x= layers.Dense(64, activation="relu")(inputs)
x2 = layers.Dense(64, activation="relu")(x)
outputs = layers.Dense(1,)(x2)
model = keras.Model(inputs=inputs, outputs=outputs, name="first_model")
x_train=np.random.rand(1,5000)
y_train=np.zeros((1,5000))
for j in range(0,np.size(x_train[0])):
y_train[0,j]=x_train[0,j]**2
model.compile(
loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
optimizer=keras.optimizers.RMSprop(),
metrics=["accuracy"],
)
history = model.fit(x_train[0], y_train[0], batch_size=10,epochs=1, )
x_test=np.random.rand(1,50000)
y_test=np.zeros((1,50000))
for j in range(0,np.size(x_test[0])):
y_test[0,j]=x_test[0,j]**2
test_scores = model.evaluate(x_test[0], y_test[0], verbose=2)
print("Test loss:", test_scores[0])
print("Test accuracy:", test_scores[1])
1条答案
按热度按时间yruzcnhs1#
您的损失函数不正确-当您要预测类别时使用
SparseCategoricalCrossentropy
。在您的情况下,您可以使用例如MeanSquaredError