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?