第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

打印張量元素 (v1.14)

打印張量元素 (v1.14)

海綿寶寶撒 2023-06-20 15:39:46
我想了解 keras/tensorflow 是如何工作的。在這個例子中,我正在使用一個LSTM具有定義loss功能的網(wǎng)絡(luò)。在此示例中,我想打印y_pred和loss變量中的值,但是標準print()函數(shù)不會打印實際數(shù)值。當我嘗試print()函數(shù)時,我得到以下輸出:Tensor("loss_13/dense_14_loss/strided_slice:0", shape=(), dtype=float32)import tensorflow as tffrom tensorflow.keras import Sequential, backend as Kfrom tensorflow.keras.layers import Dense, LSTM, Dropoutfrom tensorflow.keras.losses import categorical_crossentropyregressor = Sequential()regressor.add(LSTM(units = 10, dropout=0.10, return_sequences = True, input_shape = (X.shape[1], X.shape[2])))regressor.add(Dense(units = 4, activation='softmax'))regressor.compile(optimizer = optimizer, loss = weight_fx(np.array([0.005,0.20,0.79,0.005])), metrics = ['categorical_accuracy'])def weight_fx(weights):    weights = K.variable(weights)         def loss(y_true, y_pred):        y_pred /= K.sum(y_pred, axis=-1, keepdims=True)        print(y_pred)        loss = y_true * K.log(y_pred) * weights        return loss        return loss
查看完整描述

2 回答

?
慕容森

TA貢獻1853條經(jīng)驗 獲得超18個贊

嘗試這樣做:


import tensorflow as tf

from tensorflow.keras import Sequential, backend as K

from tensorflow.keras.layers import Dense, LSTM, Dropout

from tensorflow.keras.losses import categorical_crossentropy

import numpy as np


X = tf.ones((10,10,10))

y = tf.ones((10,1))

def weight_fx(weights):

? ? weights = K.variable(weights)? ? ?

? ? def loss(y_true, y_pred):

? ? ? ? y_pred /= K.sum(y_pred, axis=-1, keepdims=True)

? ? ? ? tf.print(y_pred)

? ? ? ? loss = y_true * K.log(y_pred) * weights

? ? ? ? return loss

? ??

? ? return loss


regressor = Sequential()

regressor.add(LSTM(units = 10, dropout=0.10, return_sequences = True,?

? ? ? ? ? ? ? ? ? ?input_shape = (X.shape[1], X.shape[2])))

regressor.add(Dense(units = 4, activation='softmax'))

regressor.compile(optimizer = 'adam', loss = weight_fx(np.array([0.005,0.20,0.79,0.005])), metrics = ['categorical_accuracy'])

regressor.fit(X,y)

  • 問:你為什么看到Tensor("loss_13/dense_14_loss/strided_slice:0", shape=(), dtype=float32)

  • 答:Tensorflow 預計損失函數(shù)會被頻繁調(diào)用,因此盡可能對其進行優(yōu)化至關(guān)重要。Tensorflow 有一種方法可以做到這一點,稱為“追蹤”。這基本上意味著傳遞一個“檢測器”變量,該變量“體驗”函數(shù)中的所有操作并記住它們。然后,基于這些經(jīng)驗,Tensorflow 構(gòu)建了一個單獨的所謂“圖形”函數(shù),該函數(shù)速度更快,并且無法調(diào)用 python 中具有副作用的許多常見函數(shù)。喜歡print()。你看到的是一個檢測器或“示蹤劑”。它只運行一次。

  • 那我該如何調(diào)試呢?

  • 有幾種方法可以做到這一點。如果要print調(diào)試,請使用tf.print.?根據(jù)我的經(jīng)驗,這有時有效,有時無效。如果沒有,并且您仍然只看到檢測器變量,請使用model.run_eagerly = True或?qū)⑵渥鳛閰?shù)傳遞給model.compile.?即使你不使用tf.print和設(shè)置run_eagerly,python 的內(nèi)置print仍然可以工作(試試這個)。最后但同樣重要的是,您可以將所有副作用函數(shù)包裝在一個tf.py_function.?

另外,請確保先定義函數(shù),然后在 中使用它model.compile,尤其是在使用 Jupyter notebook 時。一個有問題的舊聲明可能仍然存在于記憶中并且可能會毀了你的一天。

這有幫助嗎?


查看完整回答
反對 回復 2023-06-20
?
慕勒3428872

TA貢獻1848條經(jīng)驗 獲得超6個贊

我還沒有嘗試過,但你應該始終使用:

tf.print(value)

而不是正常的

print(value)
查看完整回答
反對 回復 2023-06-20
  • 2 回答
  • 0 關(guān)注
  • 153 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號