2017-11-22 1 views
0

Ich habe einen Leitfaden Start für Tensorflow hier gelesen a link In diesem Handbuch verwenden Sie ein Beispiel für eine Dimension für das Modell y = W * x + b. Danach habe ich versucht, 2-dimensional für x zu erstellen. Folgen Sie ist mein Code:Wie erstellen Sie ein lineares Regressionsmodell für multidimensionale Daten in Tensorflow?

import tensorflow as tf 
import numpy as np 
import random as rd 
rd.seed(2) 

#model is 2*x1 + x2 - 3 = y 
def create_data_train(): 
x_train = np.asarray([[2,3],[6,7],[1,5],[4,6],[10,-1],[0,0],[5,6], 
[8,9],[4.5,6.2],[1,1],[0.3,0.2]]) 
w_train = np.asarray([[2,1]]) 
b = np.asarray([[-3]]) 
y_train = np.dot(x_train, w_train.T) + b 
for i in range(y_train.shape[0]): 
    for j in range(y_train.shape[1]): 
     y_train[i][j] += 1-rd.randint(0,2) 
return x_train,y_train 

# step 1 
x = tf.placeholder(tf.float32, [None, 2]) 
W = tf.Variable(tf.zeros([2, 1])) 
b = tf.Variable(tf.zeros([1])) 
y = tf.matmul(x, W) + b 
y_ = tf.placeholder(tf.float32, [None, 1]) 
# step 2 
loss = tf.reduce_sum(tf.pow(tf.subtract(y,y_),2)) 
optimizer = tf.train.GradientDescentOptimizer(0.05) 
train = optimizer.minimize(loss) 
#step 3 
x1,y1 = create_data_train() 
x_train = tf.convert_to_tensor(x1) 
y_train = tf.convert_to_tensor(y1) 
print(x_train) 
print(y_train) 
init = tf.global_variables_initializer() 
sess = tf.Session() 
print(sess.run(x_train)) 
sess.run(init) 
for i in range(1000): 
    sess.run(train,feed_dict={x:x_train,y_:y_train}) 

endW ,endb = sess.run([W,b]) 
print(endW) 
print(endb) 

Aber wenn ich laufe, ich ist ein Fehler aufgetreten ist:

TypeError: The value of a feed cannot be a tf.Tensor object. Acceptable feed values include Python scalars, strings, lists, numpy ndarrays, or TensorHandles.

Antwort

0

Der Fehler hier, weil Sie keinen Tensor feed_dict ernähren kann.

Da Sie zwei Linien,

folgende haben
x_train = tf.convert_to_tensor(x1) 
y_train = tf.convert_to_tensor(y1) 

Sie konvertieren x1 und y1 zu Tensoren (d.h Ihre Ein- und Ausgänge). Feed x1 und y1 direkt ohne Umwandlung in Tensoren.

Hoffe, das hilft.

Verwandte Themen