douchungu0859
douchungu0859
2017-09-22 15:44
浏览 79

无法使用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
    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/

    点赞 评论

相关推荐