douchungu0859 2017-09-22 15:44
浏览 94
已采纳

无法使用TensorFlow Go API进行预测

I have a MLP coded using Tensorflow Python API. The following is the code snippet:

# tf Graph input
x = tf.placeholder("float", [None, 11],name="x")
y = tf.placeholder("float", [None])

# Store layers weight & bias
weights = {
    'h1': tf.Variable(tf.random_normal([11, 32], 0, 0.1)),
    'h2': tf.Variable(tf.random_normal([32, 200], 0, 0.1)),
    'out': tf.Variable(tf.random_normal([200, 1], 0, 0.1))
}

biases = {
    'b1': tf.Variable(tf.random_normal([32], 0, 0.1)),
    'b2': tf.Variable(tf.random_normal([200], 0, 0.1)),
    'out': tf.Variable(tf.random_normal([1], 0, 0.1))
}

# Create model
def multilayer_perceptron(x, weights, biases):
    # Hidden layer with RELU activation
    layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])
    layer_1 = tf.nn.relu(layer_1)

    # Hidden layer with RELU activation
    layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])
    layer_2 = tf.nn.relu(layer_2)

    # Output layer with linear activation
    out_layer = tf.matmul(layer_2, weights['out']) + biases['out']
    return out_layer

# Construct model
pred = multilayer_perceptron(x, weights, biases)
pred = tf.identity(pred, name="pred")

The model has been trained and saved using the saved_model_builder.SavedModelBuilder method. The predictions using the Python API can be done using the following code:

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    tf.saved_model.loader.load(sess, ["tag"], "/tmp/saved_models")
    my_pred= sess.graph.get_tensor_by_name('pred:0')
    predictions = sess.run(my_pred, feed_dict={x: pred_data})
    print("The prediction is", predictions)

I am trying to make the same predictions using the Go API using the following code snippet:

df := []float32{9.5,0.0,7.5,0.0,0.0,2.0,0.0,0.0,0.0,0.0,1505292248.0}
 tensor, terr := tf.NewTensor(df) 
 result, runErr := model.Session.Run(
     map[tf.Output]*tf.Tensor{
         model.Graph.Operation("x").Output(0): tensor,
     },
     []tf.Output{
         model.Graph.Operation("pred").Output(0),
     },
     nil,
   )

However, I encounter with the following error:

Error running the session with input, err: In[0] is not a matrix
         [[Node: MatMul = MatMul[T=DT_FLOAT, _output_shapes=[[?,32]], transpose_a=false, transpose_b=false, _device="/job:localhost/replica:0/task:0/cpu:0"](_arg_x_0_0, Variable/read)]]

Can someone point out the reason for this error?

  • 写回答

1条回答 默认 最新

  • dongrunying7537 2017-09-22 16:16
    关注

    The error is clear: In[0] is not a matrix.

    Your In[0] is: df := []float32{9.5,0.0,7.5,0.0,0.0,2.0,0.0,0.0,0.0,0.0,1505292248.0}

    This is a 1-dimensional tensor, not a matrix.

    The matmul node requires both its arguments to be matrices, thus 2-dimensional tensors.

    Therefore, you have to change your df definition in order to define a 2-D tensor, like that:

    df := [][]float32{{9.5},{0.0},{7.5},{0.0},{0.0},{2.0},{0.0},{0.0},{0.0},{0.0},{1505292248.0}}
    

    A good reference on how to think/debug tensorflow + go code is: https://pgaleone.eu/tensorflow/go/2017/05/29/understanding-tensorflow-using-go/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制