尝试使用训练数据时出现奇怪的错误

时间:2020-06-12 00:05:29

标签: python tensorflow keras tf.keras

当尝试运行以下代码时(我已将数据替换为1,2,3 ...数字,以使问题可重现)

x_train = np.array([[1,2,3,4,5,6,7,8,9,10]]*1000).astype("float32")
y_train = np.asarray([1.0]*1000).astype("float32")
model = tf.keras.models.Sequential([
  tf.keras.layers.Input(shape=(10,)),
  tf.keras.layers.Dense(128,activation='relu'),
  tf.keras.layers.Dense(128,activation='relu'),
  tf.keras.layers.Dense(1, activation='softmax')
])
model.compile(
    loss=tf.keras.losses.MeanSquaredError,
    optimizer=tf.keras.optimizers.Adam(0.001),
    metrics=['mae'],
)
model.fit(x=x_train,y=y_train,epochs=5)

我从最后一行抛出TypeError: Expected float32 passed to parameter 'y' of op 'Equal', got 'auto' of type 'str' instead. Error: Expected float32, got 'auto' of type 'str' instead.

该错误消息是毫无意义的,因为没有字符串,所以我的问题是:

  1. 发生了什么事?
  2. 如何克服这个问题并适合模型代码中指定的x_trainy_train

1 个答案:

答案 0 :(得分:0)

别忘了实例化您的损失:)

var result = await graphClient.Users.Request().AddAsync(new User()
            {
                AccountEnabled = true,
               
                MailNickname = "example",
                UserPrincipalName = "example665@XX.live",
                Surname = "TEST",
                DisplayName = "test",
                GivenName = "TEST test",
                PasswordProfile = new PasswordProfile
                {
                    ForceChangePasswordNextSignIn = true,
                    Password = "password@1234"
                }
            });
            Console.WriteLine(JsonConvert.SerializeObject(result));
        }