简体   繁体   English

ValueError:形状必须为 2 级,但对于 'in_top_k/InTopKV2'(操作:'InTopKV2')为 4 级,输入形状为:[?,28,28,10], [?], []

[英]ValueError: Shape must be rank 2 but is rank 4 for 'in_top_k/InTopKV2' (op: 'InTopKV2') with input shapes: [?,28,28,10], [?], []

I'm new to Tensorflow and I am trying to train on MNIST.我是 Tensorflow 的新手,我正在尝试在 MNIST 上进行训练。 However, the code fails on但是,代码失败了

correct = tf.nn.in_top_k(logits, tf.argmax(y, axis=1), 1)

with the error "ValueError: Shape must be rank 2 but is rank 4 for 'in_top_k/InTopKV2' (op: 'InTopKV2') with input shapes: [?,28,28,10], [?], []"出现错误“ValueError:Shape must be rank 2 but is rank 4 for 'in_top_k/InTopKV2' (op: 'InTopKV2') with input shapes: [?,28,28,10], [?], []”

What is going on here, and what do I need to know to make this compatible with different architectures in the future?这里发生了什么,我需要知道什么才能使其与未来的不同架构兼容? I've included the entire file below.我在下面包含了整个文件。

import tensorflow as tf
import numpy as np
from tensorflow.python.framework import graph_util
from tensorflow.python.framework import graph_io

tf.reset_default_graph()

x = tf.placeholder(tf.float32, shape=(None, 28, 28), name='x_input')
y = tf.placeholder(tf.float32, shape=(None, 10), name='y_label')
y = tf.stop_gradient(y, name="stop_gradient_y")

input_layer = tf.reshape(x, [-1, 28, 28, 1], name='x_reshaped')

fc_layer1 = tf.layers.dense(
        inputs=input_layer, units=1024, activation=tf.nn.relu, name='fc_layer_1')

fc_layer2 = tf.layers.dense(
        inputs=fc_layer1, units=512, activation=tf.nn.relu, name='fc_layer_2')

fc_layer3 = tf.layers.dense(
        inputs=fc_layer2, units=512, activation=tf.nn.relu, name='fc_layer_3')

fc_layer4 = tf.layers.dense(
        inputs=fc_layer3, units=512, activation=tf.nn.relu, name='fc_layer_4')

fc_layer5 = tf.layers.dense(
        inputs=fc_layer4, units=512, activation=tf.nn.relu, name='fc_layer_5')

logits = tf.layers.dense(inputs=fc_layer5, units=10, name='logits')
classes = tf.argmax(input=logits, axis=1, name='classes')
probabilities = tf.nn.softmax(logits, name="probabilities_out")
loss = tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=logits, name='loss_func')
grad = tf.gradients(loss, x)
grad_out = tf.identity(grad, name='gradient_out')

optimizer = tf.train.AdamOptimizer()
train_op = optimizer.minimize(loss)
correct = tf.nn.in_top_k(logits, tf.argmax(y, axis=1), 1)
accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))

(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
x_train = x_train/np.float32(255)
y_train = y_train.astype(np.int32)
x_test = x_test/np.float32(255)
y_test = y_test.astype(np.int32)

y_train = tf.keras.utils.to_categorical(y_train, 10)
y_test = tf.keras.utils.to_categorical(y_test, 10)

num_epochs = 100
batch_size = 100

init = tf.global_variables_initializer()

with tf.Session() as sess:
    init.run()
    for epoch in range(num_epochs):
        print('Epoch: {}'.format(epoch))
        for i in range(x_train.shape[0] // batch_size):
            batch_indices = np.random.randint(x_train.shape[0], size=batch_size)
            x_batch = x_train[batch_indices]
            y_batch = y_train[batch_indices]
            sess.run(train_op, feed_dict={x: x_batch, y: y_batch})
        acc_test = accuracy.eval(feed_dict={x: x_test, y: y_test})
        print(epoch, "Test accuracy:", acc_test)

    constant_graph = graph_util.convert_variables_to_constants(
            sess, 
            sess.graph.as_graph_def(), 
            ['probabilities_out', 'gradient_out'])

    graph_io.write_graph(constant_graph, '.', 'mnist_gradient_fc_without.pb', as_text=False)

Thanks JacoSolari and Jarom Allen.感谢 JacoSolari 和 Jarom Allen。 For the benefit of community providing complete working code here为了社区的利益,在这里提供完整的工作代码

import tensorflow as tf
import numpy as np
from tensorflow.python.framework import graph_util
from tensorflow.python.framework import graph_io

tf.reset_default_graph()

x = tf.placeholder(tf.float32, shape=(None, 28, 28), name='x_input')
y = tf.placeholder(tf.float32, shape=(None, 10), name='y_label')
y = tf.stop_gradient(y, name="stop_gradient_y")

input_layer = tf.reshape(x, [-1, 784 ], name='x_reshaped')


fc_layer1 = tf.layers.dense(
        inputs=input_layer, units=1024, activation=tf.nn.relu, name='fc_layer_1')

fc_layer2 = tf.layers.dense(
        inputs=fc_layer1, units=512, activation=tf.nn.relu, name='fc_layer_2')

fc_layer3 = tf.layers.dense(
        inputs=fc_layer2, units=512, activation=tf.nn.relu, name='fc_layer_3')

fc_layer4 = tf.layers.dense(
        inputs=fc_layer3, units=512, activation=tf.nn.relu, name='fc_layer_4')

fc_layer5 = tf.layers.dense(
        inputs=fc_layer4, units=512, activation=tf.nn.relu, name='fc_layer_5')

logits = tf.layers.dense(inputs=fc_layer5, units=10, name='logits')
classes = tf.argmax(input=logits, axis=1, name='classes')
probabilities = tf.nn.softmax(logits, name="probabilities_out")
loss = tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=logits, name='loss_func')
grad = tf.gradients(loss, x)
grad_out = tf.identity(grad, name='gradient_out')

optimizer = tf.train.AdamOptimizer()
train_op = optimizer.minimize(loss)
correct = tf.nn.in_top_k(logits, tf.argmax(y, axis=1), 1)
accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))

(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
x_train = x_train/np.float32(255)
y_train = y_train.astype(np.int32)
x_test = x_test/np.float32(255)
y_test = y_test.astype(np.int32)

y_train = tf.keras.utils.to_categorical(y_train, 10)
y_test = tf.keras.utils.to_categorical(y_test, 10)

num_epochs = 5
batch_size = 100

init = tf.global_variables_initializer()

with tf.Session() as sess:
    init.run()
    for epoch in range(num_epochs):
        print('Epoch: {}'.format(epoch))
        for i in range(x_train.shape[0] // batch_size):
            batch_indices = np.random.randint(x_train.shape[0], size=batch_size)
            x_batch = x_train[batch_indices]
            y_batch = y_train[batch_indices]
            sess.run(train_op, feed_dict={x: x_batch, y: y_batch})
        acc_test = accuracy.eval(feed_dict={x: x_test, y: y_test})
        print(epoch, "Test accuracy:", acc_test)

    constant_graph = graph_util.convert_variables_to_constants(
            sess, 
            sess.graph.as_graph_def(), 
            ['probabilities_out', 'gradient_out'])

    graph_io.write_graph(constant_graph, '.', 'mnist_gradient_fc_without.pb', as_text=False)

Output: Output:

Epoch: 0
0 Test accuracy: 0.9527
Epoch: 1
1 Test accuracy: 0.9683
Epoch: 2
2 Test accuracy: 0.9731
Epoch: 3
3 Test accuracy: 0.9776
Epoch: 4
4 Test accuracy: 0.9821
INFO:tensorflow:Froze 12 variables.
INFO:tensorflow:Converted 12 variables to const ops.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:形状必须为 0 级,但对于“ReadFile”(操作:“ReadFile”)为 1 级,输入形状为:[1] - ValueError: Shape must be rank 0 but is rank 1 for 'ReadFile' (op: 'ReadFile') with input shapes: [1] Tensorflow-ValueError:形状必须为0级,但输入范围为[],[10],[]的“范围”(操作数:“范围”)的“极限”必须为1级 - Tensorflow - ValueError: Shape must be rank 0 but is rank 1 for 'limit' for 'range' (op: 'Range') with input shapes: [], [10], [] tf.nn.in_top_k 给出错误,我不知道为什么:NotFoundError:找不到节点的有效设备。 节点:{{node InTopKV2}} - tf.nn.in_top_k gives error and I don't know why: NotFoundError: Could not find valid device for node. Node:{{node InTopKV2}} ValueError: Shape must be rank 2 but is rank 1 for 'MatMul' (op: 'MatMul') with input shape: [2], [2,3] - ValueError: Shape must be rank 2 but is rank 1 for 'MatMul' (op: 'MatMul') with input shapes: [2], [2,3] ValueError:Shape必须为1级,但对于'ROIAlign / Crop'(op:'CropAndResize')排名为0,输入形状为:[2,360,475,3],[1,4],[],[2] - ValueError: Shape must be rank 1 but is rank 0 for 'ROIAlign/Crop' (op: 'CropAndResize') with input shapes: [2,360,475,3], [1,4], [], [2] 形状必须为2级,但输入形状为[100,100],[?, 15,100]的'MatMul_46'(op:'MatMul')的等级为3 - Shape must be rank 2 but is rank 3 for 'MatMul_46' (op: 'MatMul') with input shapes: [100,100], [?,15,100] ValueError:`logits` 和 `labels` 必须具有相同的形状,收到 ((100, 28, 28, 10) vs (100, 10)) - ValueError: `logits` and `labels` must have the same shape, received ((100, 28, 28, 10) vs (100, 10)) ValueError:形状必须为 2 级,但“MatMul”为 1 级 - ValueError: Shape must be rank 2 but is rank 1 for 'MatMul' TensorFlow 推荐者 - ValueError:形状必须为 2 级,但为 3 级 - TensorFlow Recommenders - ValueError: Shape must be rank 2 but is rank 3 Tensorflow: ValueError: Shape must be rank 4 but is rank 5 - Tensorflow: ValueError: Shape must be rank 4 but is rank 5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM