今天在写代码时遇到了这个问题,查了很多资料才解决,记一下。以防以后再出现。
import numpy as np
import tensorflow as tf
from tensorflow.python.keras.layers import Dense
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.optimizers import Adam
model = Sequential([
Dense(24, input_dim=4, activation='relu'),
Dense(24, activation='relu'),
Dense(2, activation='linear')
])
model.compile(loss='mse', optimizer=Adam(lr=0.001))
x = np.array([[-0.08623559, -0.79897248, 0.03606475, 1.09068178]])
y = np.array([[ 1.0449973, -0.14471795]])
model.fit(x, y)
运行时报错:
Tried to convert 'y' to a tensor and failed. Error: None values not supported
解决办法如下:
这是tensorflow版本问题,更改imports如下(去掉.python):
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Sequential
from tensorflow.keras.optimizers import Adam
正常运行:
Train on 1 samples
1/1 [==============================] - 0s 239ms/sample - loss: 0.5793
<tensorflow.python.keras.callbacks.History at 0x7fa54c4f6630>