无法采用未知等级的Shape的长度 [英] Cannot take the length of Shape with unknown rank
问题描述
我有一个神经网络,它来自一个 tf.data
数据生成器和一个 tf.keras
模型,如下所示(简化的版本,因为它太长了):
dataset = ...
使用 next_x
方法的 tf.data.Dataset
对象为 x_train
迭代器和 next_y
调用 get_next
方法为 y_train
迭代器调用 get_next
。每个标签都是一个(1,67)
数组,形式为一格。
图层:
input_tensor = tf.keras.layers.Input(shape =(240,240,3))#x的暗淡
输出= tf.keras.layers.Flatten()(input_tensor)
output = tf.keras.Dense(67,activation ='softmax')(output)#67是
$ c类的数量$ c>
型号:
model = tf .keras.models.Model(输入=输入张量,输出=预测)
model.compile(optimizer = tf.train.AdamOptimizer(),loss = tf.losses.softmax_cross_entropy,metrics = ['accuracy'])
model.fit_generator(gen(dataset.next_x(),dataset.next_y()),steps_per_epochs = 100)
gen
的定义如下:
def gen [x,y):
而True:
收益率(x,y)
我的问题是,当我尝试运行它时,在 model.fit
部分出现错误:
ValueError:不能
我发现了问题所在。实际上,在产生它之前,我必须在 tf.Session
中进行运行
的下一批。
这是它的工作方式(我不会编写其余代码,因为它保持不变):
model.fit_generator(gen(),steps_per_epochs = 100)
def gen():
,其中tf.Session()为sess:
next_x = dataset.next_x( )
next_y =数据集.next_y()
而True:
x_batch = sess.run(next_x)
y_batch = sess.run(next_y)
收益x_batch y_batch
I have a neural network, from a tf.data
data generator and a tf.keras
model, as follows (a simplified version-because it would be too long):
dataset = ...
A tf.data.Dataset
object that with the next_x
method calls the get_next
for the x_train
iterator and for the next_y
method calls the get_next
for the y_train
iterator. Each label is a (1, 67)
array in one-hot form.
Layers:
input_tensor = tf.keras.layers.Input(shape=(240, 240, 3)) # dim of x
output = tf.keras.layers.Flatten()(input_tensor)
output= tf.keras.Dense(67, activation='softmax')(output) # 67 is the number of classes
Model:
model = tf.keras.models.Model(inputs=input_tensor, outputs=prediction)
model.compile(optimizer=tf.train.AdamOptimizer(), loss=tf.losses.softmax_cross_entropy, metrics=['accuracy'])
model.fit_generator(gen(dataset.next_x(), dataset.next_y()), steps_per_epochs=100)
gen
is defined like this:
def gen(x, y):
while True:
yield(x, y)
My problem is that when I try to run it, I get an error in the model.fit
part:
ValueError: Cannot take the length of Shape with unknown rank.
Any ideas are appreciated!
I found out what was wrong. Actually I have to run
next batch in a tf.Session
before yielding it.
Here is how it works (I don't write the rest of the code, since it stays the same):
model.fit_generator(gen(), steps_per_epochs=100)
def gen():
with tf.Session() as sess:
next_x = dataset.next_x()
next_y = dataset.next_y()
while True:
x_batch = sess.run(next_x)
y_batch = sess.run(next_y)
yield x_batch, y_batch
这篇关于无法采用未知等级的Shape的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!