weixin_39669761
2020-11-27 21:03 阅读 106

Predict error: Tensor conversion requested dtype int32 for Tensor with dtype float64

I'm trying to expand the gpFlow tutorial to a multivariate problem (20 dimensions) but keep getting this error when I try to predict using: mean, var = m.predict_y(Xtest)

Error message: ValueError: Tensor conversion requested dtype int32 for Tensor with dtype float64: 'Tensor("zeros:0", shape=(?, 1), dtype=float64)'

This even happens when I feed my training data back into the predict function. Is there an example/tutorial for a multivariate problem I can follow? Or any clues on the above error?

(Not sure how to label this as a discussion)

该提问来源于开源项目:GPflow/GPflow

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

6条回答 默认 最新

  • weixin_39593247 weixin_39593247 2020-11-27 21:03

    What data type is Xtest? If you do e.g.Xtest = np.array((1, 2, 3)) you'll get this sort of error, which can corrected with Xtest = np.array((1, 2, 3), dtype=float)

    点赞 评论 复制链接分享
  • weixin_39669761 weixin_39669761 2020-11-27 21:03

    Xtest looks like this:

    
    array([[ 4.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
             0.,  0.,  0.,  0.,  1.,  1.,  1.],
           [ 4.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
             0.,  0.,  1.,  0.,  1.,  1.,  1.]])
    
    点赞 评论 复制链接分享
  • weixin_39990558 weixin_39990558 2020-11-27 21:03

    Oh. I'm not sure what the problem is then. Could you post a minimum working (non-working) example, please?

    点赞 评论 复制链接分享
  • weixin_39669761 weixin_39669761 2020-11-27 21:03

    Here's a version of my code. I've found it works ok on dummy data (option 2) but not on my real data (options 3-5). Am still investigating ...

    
    from __future__ import print_function
    import GPflow
    import tensorflow as tf
    import matplotlib
    import numpy as np
    import matplotlib.pyplot as plt
    %matplotlib inline
    
    option = 3
    
    if option ==1:  # Tutorial, dummy data, 1 dimension - definitely works
        N = 2000
        Xtrain = np.random.rand(N,1)
        Ytrain = np.sin(12*Xtrain) + 0.66*np.cos(25*Xtrain) + np.random.randn(N,1)*0.1 + 3
        batch_size = 300
        display_step = 2
    elif option ==2: # Use dummy data, 20 dimensions
        N = 50000
        Xtrain = np.round(np.random.random((N,20)),0)  # This works. Use this line if you wish to test.
        Ytrain = np.round(np.random.random((N,1)),0)  # This works. Use this line if you wish to test.
    elif option ==3: # Use subset of real data
        N=1000
        Xtrain = realDataX[0:N] 
        Ytrain = realDataY[0:N] 
    elif option == 4: #Do nothing / Use full data
        N= np.shape(realData)[0]
        Xtrain = realDataX[0:N] 
        Ytrain = realDataY[0:N] 
    elif option == 5: #Sometimes works, sometimes get either of the two errors
        N= np.shape(realDataX)[0]
        Xtrain = realDataX
        Ytrain = realDataY
    
    batch_size = int(N/5)
    training_iters = np.shape(Xtrain)[0]-batch_size
    display_step = 500
    
    
    # Network Parameters
    n_input = np.shape(Xtrain)[1]
    n_steps = 1 # timesteps
    n_hidden = 128 # hidden layer num of features
    n_classes = 2
    
    step=1
    
    
    # Keep training until reach max iterations
    while step * batch_size < training_iters:
        if step%display_step == 0: print("Step: {}".format(step))
        X = Xtrain[step*batch_size:(step*batch_size)+batch_size]
        Y = Ytrain[step*batch_size:(step*batch_size)+batch_size]  
    
        k = GPflow.kernels.Matern52(1, lengthscales=0.3)
        m = GPflow.gpr.GPR(X, Y, kern=k)
        m.likelihood.variance = 0.01
    
    
        step += 1
    
    mean, var = m.predict_y(Xtrain[2:3])  # One row of input
    
    print(mean,var)
    
    

    Error:

    
    ValueError                                Traceback (most recent call last)
    <ipython-input-489-9aa3e8333856> in <module>()
         59     step += 1
         60 
    ---> 61 mean, var = m.predict_y(Xtrain[2:3])  # One row of input
         62 
         63 print(mean,var)
    
    ~/anaconda3/envs/tensorflow/lib/python3.5/site-packages/GPflow-0.3.8-py3.5.egg/GPflow/param.py in runnable(instance, *np_args)
        559                     instance.make_tf_array(storage['free_vars'])
        560                     with instance.tf_mode():
    --> 561                         storage['tf_result'] = tf_method(instance, *storage['tf_args'])
        562                     storage['feed_dict_keys'] = instance.get_feed_dict_keys()
        563                     feed_dict = {}
    
    ~/anaconda3/envs/tensorflow/lib/python3.5/site-packages/GPflow-0.3.8-py3.5.egg/GPflow/model.py in predict_y(self, Xnew)
        402         Compute the mean and variance of held-out data at the points Xnew
        403         """
    --> 404         pred_f_mean, pred_f_var = self.build_predict(Xnew)
        405         return self.likelihood.predict_mean_and_var(pred_f_mean, pred_f_var)
        406 
    
    ~/anaconda3/envs/tensorflow/lib/python3.5/site-packages/GPflow-0.3.8-py3.5.egg/GPflow/gpr.py in build_predict(self, Xnew, full_cov)
         78         L = tf.cholesky(K)
         79         A = tf.matrix_triangular_solve(L, Kx, lower=True)
    ---> 80         V = tf.matrix_triangular_solve(L, self.Y - self.mean_function(self.X))
         81         fmean = tf.matmul(A, V, transpose_a=True) + self.mean_function(Xnew)
         82         if full_cov:
    
    ~/anaconda3/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/ops/math_ops.py in binary_op_wrapper(x, y)
        818     with ops.name_scope(None, op_name, [x, y]) as name:
        819       if not isinstance(y, sparse_tensor.SparseTensor):
    --> 820         y = ops.convert_to_tensor(y, dtype=x.dtype.base_dtype, name="y")
        821       return func(x, y, name=name)
        822 
    
    ~/anaconda3/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/framework/ops.py in convert_to_tensor(value, dtype, name, preferred_dtype)
        637       name=name,
        638       preferred_dtype=preferred_dtype,
    --> 639       as_ref=False)
        640 
        641 
    
    ~/anaconda3/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/framework/ops.py in internal_convert_to_tensor(value, dtype, name, as_ref, preferred_dtype)
        702 
        703         if ret is None:
    --> 704           ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
        705 
        706         if ret is NotImplemented:
    
    ~/anaconda3/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/framework/ops.py in _TensorTensorConversionFunction(t, dtype, name, as_ref)
        575     raise ValueError(
        576         "Tensor conversion requested dtype %s for Tensor with dtype %s: %r"
    --> 577         % (dtype.name, t.dtype.name, str(t)))
        578   return t
        579 
    
    ValueError: Tensor conversion requested dtype int32 for Tensor with dtype float64: 'Tensor("zeros:0", shape=(?, 1), dtype=float64)'
    </module></ipython-input-489-9aa3e8333856>
    点赞 评论 复制链接分享
  • weixin_39593247 weixin_39593247 2020-11-27 21:03

    If you change the line

    python
    m = GPflow.gpr.GPR(X, Y, kern=k)
    

    to

    python
    m = GPflow.gpr.GPR(np.array(X, dtype=float), np.array(Y, dtype=float), kern=k)
    

    do you still get the error?

    点赞 评论 复制链接分享
  • weixin_39669761 weixin_39669761 2020-11-27 21:03

    Ok that appears to have worked. I did a check on a random element of my data and it was float64 so not sure why it didn't work before. But all good now. Will close this issue once you've had a chance to reply. Thanks for your help.

    点赞 评论 复制链接分享

相关推荐