TensorFlow 문서에 표시된 입력 파이프 라인에 대해 Dataset API를 시도 하고 있으며 거의 동일한 코드를 사용합니다.
tr_data = Dataset.from_tensor_slices((train_images, train_labels))
tr_data = tr_data.map(input_parser, NUM_CORES, output_buffer_size=2000)
tr_data = tr_data.batch(BATCH_SIZE)
tr_data = tr_data.repeat(EPOCHS)
iterator = dataset.make_one_shot_iterator()
next_example, next_label = iterator.get_next()
# Script throws error here
loss = model_function(next_example, next_label)
with tf.Session(...) as sess:
sess.run(tf.global_variables_initializer())
while True:
try:
train_loss = sess.run(loss)
except tf.errors.OutOfRangeError:
print("End of training dataset.")
break
느린 feed_dicts를 사용하지 않기 때문에 더 빨라야합니다. 그러나 단순화 된 LeNet 아키텍처 인 내 모델에서는 작동하도록 만들 수 없습니다. 문제는 (가)입니다 tf.layers.dense
내에서 model_function()
어떤 알려진 입력 형태 (이 사전에 무게의 수를 알고 있기 때문에 내가 추측) 기대하고있다. 그러나 next_example
및 next_label
단지 세션을 실행하여 자신의 모양을 얻을. 평가하기 전에 모양이 정의되지 않았습니다.?
선언하면 model_function()
다음 오류가 발생합니다.
ValueError : 입력의 마지막 차원을
Dense
정의해야합니다. 을 찾았습니다None
.
지금은이 Dataset API를 의도 한 방식으로 사용하고 있는지 아니면 해결 방법이 있는지 모르겠습니다.
미리 감사드립니다!
편집 1 : 아래는 내 모델이며 첫 번째 고밀도 레이어에서 오류가 발생합니다.
def conv_relu(input, kernel_shape):
# Create variable named "weights".
weights = tf.get_variable("weights", kernel_shape,
initializer=tf.random_normal_initializer())
# Create variable named "biases".
biases = tf.get_variable("biases", kernel_shape[3],
initializer=tf.constant_initializer(0.0))
conv = tf.nn.conv2d(input, weights,
strides=[1, 1, 1, 1], padding='VALID')
return tf.nn.relu(conv + biases)
def fully(input, output_dim):
assert len(input.get_shape())==2, 'Wrong input shape, need flattened tensor as input'
input_dim = input.get_shape()[1]
weight = tf.get_variable("weight", [input_dim, output_dim],
initializer=tf.random_normal_initializer())
bias = tf.get_variable('bias', [output_dim],
initializer=tf.random_normal_initializer())
fully = tf.nn.bias_add(tf.matmul(input, weight), bias)
return fully
def simple_model(x):
with tf.variable_scope('conv1'):
conv1 = conv_relu(x, [3,3,1,10])
conv1 = tf.nn.max_pool(conv1,[1,2,2,1],[1,2,2,1],'SAME')
with tf.variable_scope('conv2'):
conv2 = conv_relu(conv1, [3,3,10,10])
conv2 = tf.nn.max_pool(conv2,[1,2,2,1],[1,2,2,1],'SAME')
with tf.variable_scope('conv3'):
conv3 = conv_relu(conv2, [3,3,10,10])
conv3 = tf.nn.max_pool(conv3,[1,2,2,1],[1,2,2,1],'SAME')
flat = tf.contrib.layers.flatten(conv3)
with tf.variable_scope('fully1'):
fully1 = tf.layers.dense(flat, 1000)
fully1 = tf.nn.relu(fully1)
with tf.variable_scope('fully2'):
fully2 = tf.layers.dense(fully1, 100)
fully2 = tf.nn.relu(fully2)
with tf.variable_scope('output'):
output = tf.layers.dense(fully2, 4)
fully1 = tf.nn.relu(output)
return output
편집 2 :
여기에서 텐서의 인쇄물을 볼 수 있습니다. next_example에는 모양이 없습니다.
next_example : Tensor ( "IteratorGetNext : 0", dtype = float32)
next_label : Tensor ( "IteratorGetNext : 1", shape = (?, 4), dtype = float32)
직접 답을 찾았습니다.
이 스레드를 따르는 쉬운 수정은 tf.Tensor.set_shape
미리 이미지 크기를 알고 있는 경우 모양을 설정하는 것 입니다.
def input_parser(img_path, label):
# read the img from file
img_file = tf.read_file(img_path)
img_decoded = tf.image.decode_image(img_file, channels=1)
img_decoded = tf.image.convert_image_dtype(img_decoded, dtype=tf.float32)
img_decoded.set_shape([90,160,1]) # This line was missing
return img_decoded, label
tensorflow 문서에이 줄이 포함되어 있으면 좋았을 것입니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다