Problem with arrays in Python 3.6

1

I started a short course on deep learning with Python, but I ended up having a problem with arrays ...   I usually pick up the class code and change it quite a bit before creating an original, but this time when I circled the code studied, it gave an error:

  

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any () or a.all ()

I tried to solve the problem in some ways, but I could not. Here is the code:

import tensorflow as tf
import numpy as np


class LinearRegression:
    def __init__(self):
        self.x_data = np.random.rand(100).astype(np.float32)
        self.y_data = self.x_data * 3 + 2
        self.y_data = np.vectorize(lambda y: y + np.random.normal(loc=0.0,scale=0.1))(self.y_data)
        self.A = tf.Variable(1.0)
        self.B = tf.Variable(0.2)
        self.Y = self.A * self.x_data + self.B
        self.loss = tf.reduce_mean(tf.square(self.Y, self.y_data))
        self.optmizer = tf.train.GradientDescentOptimizer(0.5)
        self.train = self.optmizer.minimize(self.loss)
        self.ini = tf.initialize_all_variables()
        self.sess = tf.Session()
        self.sess.run(self.ini)
        self.train_data = []
        for step in range(0, 100):
            self.ev = self.sess.run([self.train, self.A, self.B])[1:]
            if step % 5 == 0:
                print(step, self.ev)
                self.train_data.append(self.ev)


Line = LinearRegression()
    
asked by anonymous 29.05.2018 / 23:16

1 answer

0

In the line: tf.square(self.Y, self.y_data) should be: tf.square(self.Y - self.y_data) .

You want to calculate the mean square error and do not pass self.y_data as the name of the operation.

    
18.09.2018 / 00:58