Reputation: 1
I want to convert a tensor object, in this case predicted values obtained by using a gaussian process model, into a numpy array.
I was writing this block of coding that goes as follows.
# Predict using the validation set
num_samples = 10
X_val_reshaped = np.ravel(X_val)
samples = gp.sample(X_val_reshaped, num_samples)
samples = tf.reshape(samples, [num_samples, -1, 1])
pred_mean = tf.reduce_mean(samples, axis=0)
pred_var = tf.math.reduce_variance(samples, axis=0)
However, I got the samples in a tensor type. I want to convert it into a numpy array so that I can calculate the RMSE value. When I try to write samples.eval() or samples.numpy(), I got error messages, such as Tensor object has no attribute 'numpy' or the other message like, No default session is registered. Use with sess.as_default()
or pass an explicit session to `eval(session=sess). And also, when I try to write these code lines
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
rmse_val = sess.run(samples)
print('samples:', samples)
it is still not a numpy object. Is there any idea how to convert a tensor object, in this case the predicted values obtained by using a gaussian process model, into a numpy object?
Upvotes: 0
Views: 39
Reputation: 151
You can directly use the tensor to compute the RMSE instead of converting it to a Numpy array.
The formula for RMSE , as you know is :
The way to implement it in TF is -
tf.sqrt(tf.reduce_mean(tf.squared_difference(Y1, Y2))).
Upvotes: 0