簡體   English   中英

Tensorflow困難進料批次大小

[英]Tensorflow difficulty feeding batch size

作為序言,我了解如何制作None大小的張量及其主要用法。

我的隱藏層取決於我要傳入的數據的batch_size。因此,我要將批次大小傳遞給占位符。 不幸的是,這給我帶來了很多壓力和錯誤,因為許多功能無法與“無”尺寸的形狀配合使用。 我正在尋找一種使用張量的動態形狀來計算隱藏層中節點數的方法。

目前,我收到錯誤消息

InvalidArgumentError(請參見上面的回溯):您必須使用dtype int32 [[Node:Placeholder_2 = Placeholderdtype = DT_INT32,shape = [],_device =“ / job:localhost / replica:0 / task)輸入占位符張量'Placeholder_2'的值:0 / CPU:0" ]]

它不喜歡使用運行時未知的形狀來初始化填充常數。 我將不勝感激幫助

以下是隔離錯誤的代碼段。

import tensorflow as tf

x = tf.placeholder(tf.float32, shape=(None, 784))

nodes = tf.div(tf.shape(x)[0],2)

bias = tf.Variable(tf.constant(.1 ,shape = [nodes]), validate_shape =  False )

init = tf.initialize_all_variables()
sess = tf.InteractiveSession()
sess.run(init)

我也收到錯誤:

shape = [int(dim)表示形狀變暗] TypeError:int()參數必須是字符串,類似字節的對象或數字,而不是'Tensor'

將填充線替換為

bias = tf.Variable(tf.constant(.1 ,shape = [nodes]), validate_shape = False )

先感謝您。

張量的形狀不能依賴於另一個張量。 形狀必須僅包含整數值或None ,表示形狀未知。 如果要對形狀執行計算,則需要使用對整數進行操作的Python原語(而不是對Tensor進行操作的Tensorflow原語)進行這些計算。

(您也不應該關閉形狀驗證---這表明您做錯了事。)

就是說,對於神經網絡中的不同層,對於批處理大小使用不同的值是不尋常的---確定要這樣做嗎?

如果確定,請嘗試以下操作:

import tensorflow as tf

batch_size = 32

x = tf.placeholder(tf.float32, shape=(batch_size, 784))
bias = tf.Variable(tf.constant(.1, shape = [batch_size / 2]))

init = tf.initialize_all_variables()
sess = tf.InteractiveSession()
sess.run(init)

希望對您有所幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM