TypeError: unsupported operand type(s) for *:


#1



My code gives this error:
TypeError: unsupported operand type(s) for *: 'NoneType' and 'float'


it is expected to print output after training


import numpy as np

'''
below this sigmoid function runs in every single neuron,
the sigmoid turns the numbers to probabilities

'''


def nonlin(x, deriv=False):
    if (deriv == True):
        return (x * (1 - x))

        return 1 / (1 + np.exp(-x))

    # when you pass in true, the drivative would be calculated,

    # input data


x = np.array([[0, 0, 1],
              [0, 0, 1],
              [1, 0, 1],
              [1, 1, 1]])

# output data
y = np.array([[0],
              [1],
              [1],
              [0]])

# seed
np.random.seed(1)

# synapses
# 1 is the bias
# syn0 is a 3 by 4 matrix
# syn2 is a 4 by 1 matrix
syn0 = 2 * np.random.random((3, 4)) - 1
syn1 = 2 * np.random.random((4, 1)) - 1

# training
for j in range(60000):

    # layers
    l0 = x
    l1 = nonlin(np.dot(l0, syn0))
    l2 = nonlin(np.dot(l1, syn1))

    # backpropagation
    l2_error = y - l2
    if j % 10000 == 0:
        print("Error:{0}".format(str(np.mean(np.abs(l2_error)))))

    # calculating deltas
    l2_delta = l2_error * nonlin(l2, deriv=True)
    l1_error = l2_delta.dot(syn1.T)
    l1_delta = l1_error * nonlin(l1, deriv=True)

    # update synapses
    syn1 += l1.T.dot(l2_delta)
    syn0 += l0.T.dot(L1_delta)

print('output after training')


#2

Hi @udori,

In the nonlin function, this line is indented too much, making it part of the if block ...

        return 1 / (1 + np.exp(-x))

This line contains a typographical error ...

    syn0 += l0.T.dot(L1_delta)

L1_delta should be l1_delta.

This is the final statement in your program ...

print('output after training')

Should some statements that display additional output follow that one?


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.