TensorFlow的逻辑回归

时间:2020-02-05 06:35:34

标签: tensorflow error-handling logistic-regression

我是TensorFlow的初学者。我想通过TensorFlow上的以下代码尝试逻辑回归。但是我不知道该如何处理错误。我对我的代码完全没有信心。如果有任何错误,请给我一些建议。

x_data=[2,2,2,2,2,3,3,3,3,3,5,5,5,5,5,6,6,6,6,6]
y_data=[1,1,0,0,0,1,0,0,0,0,1,1,1,0,0,1,1,1,1,0]
b = tf.Variable([1.0])
a = tf.Variable([1.0])
eta = a + b * x_data
p = 1/(1+tf.math.exp(-eta))
xxx = -tf.reduce_sum((y_data * tf.log(p) + (1 - y_data) * tf.log(1 - p)))

#TypeError: unsupported operand type(s) for -: 'int' and 'list'

1 个答案:

答案 0 :(得分:0)

要解决该错误,需要将输入x_datay_data转换为TensorFlow tensor,如下所示:

x_data=tf.constant([2,2,2,2,2,3,3,3,3,3,5,5,5,5,5,6,6,6,6,6], dtype='float32')
y_data=tf.constant([1,1,0,0,0,1,0,0,0,0,1,1,1,0,0,1,1,1,1,0], dtype='float32')

下面是结果xxx的屏幕截图:

enter image description here