Problem with arrays in Python 3.6

Asked

Viewed 1,199 times

2

I started a small course on deep learning with Python, but I ended up having a problem with arrays... I usually take the code from the class and modify it a lot before creating an original, but this time, when I ran the code studied, it made a mistake:

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 a few ways, but I couldn’t. Here’s 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()
  • And what line went wrong?

  • Line 13 :self.Oss = Tf.reduce_mean(Tf.square(self. Y, self.y_data))

1 answer

1

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

You want to calculate the average quadratic error and not pass self.y_data as the name of the operation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.