亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

numpy 數組輸入到 tensorflow/keras 神經網絡的 dtype 有什么關系嗎?

numpy 數組輸入到 tensorflow/keras 神經網絡的 dtype 有什么關系嗎?

慕沐林林 2023-05-23 10:39:57
如果我采用 tensorflow.keras 模型并調用model.fit(x, y)(numpy 數組在哪里x),numpy 數組是y什么重要嗎?dtype我最好只是盡可能dtype?。ɡ鏸nt8二進制數據),還是這會給 tensorflow/keras 額外的工作以將其轉換為浮點數?
查看完整描述

1 回答

?
慕田峪4524236

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 傳遞給基礎層構造函數來禁用自動轉換。


查看完整回答
反對 回復 2023-05-23
  • 1 回答
  • 0 關注
  • 112 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號