1 回答

TA貢獻1875條經驗 獲得超5個贊
您應該將輸入轉換為np.float32,這是 Keras 的默認數據類型。查一下:
import tensorflow as tf
tf.keras.backend.floatx()
'float32'
如果你給 Keras 輸入 in np.float64,它會抱怨:
import tensorflow as tf
from tensorflow.keras.layers import Dense?
from tensorflow.keras import Model
from sklearn.datasets import load_iris
iris, target = load_iris(return_X_y=True)
X = iris[:, :3]
y = iris[:, 3]
ds = tf.data.Dataset.from_tensor_slices((X, y)).shuffle(25).batch(8)
class MyModel(Model):
? def __init__(self):
? ? super(MyModel, self).__init__()
? ? self.d0 = Dense(16, activation='relu')
? ? self.d1 = Dense(32, activation='relu')
? ? self.d2 = Dense(1, activation='linear')
? def call(self, x):
? ? x = self.d0(x)
? ? x = self.d1(x)
? ? x = self.d2(x)
? ? return x
model = MyModel()
_ = model(X)
警告:tensorflow:Layer my_model 正在將輸入張量從 dtype float64 轉換為層的 dtype float32,這是 TensorFlow 2 中的新行為。該層具有 dtype float32,因為它的 dtype 默認為 floatx。如果你打算在 float32 中運行這個層,你可以安全地忽略這個警告。如果有疑問,如果您將 TensorFlow 1.X 模型移植到 TensorFlow 2,則此警告可能只是一個問題。要將所有層更改為默認 dtype float64,請調用tf.keras.backend.set_floatx('float64'). 要僅更改這一層,請將 dtype='float64' 傳遞給層構造函數。如果您是該層的作者,則可以通過將 autocast=False 傳遞給基礎層構造函數來禁用自動轉換。
添加回答
舉報