Python Forum
error "list object has no attribute transpose()"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
error "list object has no attribute transpose()"
#1
Hi,

I am trying to build a neural network this is my code

import numpy as np
import random

class QuadraticCost(object):
    @staticmethod
    def cost(a,y):
        return 0.5 * np.linalg.norm(a-y) ** 2

    def delta_L(a,y,z):
        return (a-y)*sigmoid_derivative(z)

class CrossEntropyCost(object):
    @staticmethod
    def cost(a,y):
        return np.sum(np.nan_to_num(-y*np.log(a)-(1-y)*np.log(1-a)))

    @staticmethod
    def delta_L(a,y,z):
        return a-y

class FullyConnectedNN(object):
    def __init__(self, sizes,cost=CrossEntropyCost):
        self.num_layers = len(sizes)
        self.sizes = sizes
        self.cost_function = cost
        self.init_weights()

    def init_weights(self):
        self.biases = [np.random.rand(bias,1) for bias in self.sizes[1:]]
        self.weights = [np.random.randn(i,j)/np.sqrt(j) for i,j in zip(self.sizes[1:],self.sizes[:-1])]

    def feedforward(self, a):
        for b, w in zip(self.biases, self.weights):
            a = self.sigmoid(np.dot(w, a)+b)
            print(len(a))
            print(a.shape)
        return a

    def SGD(self,training_data,learning_rate,batch_size,epochs,test_data=None):
        for i in range(epochs):
            print("Epoch " + str(i))
            #for some reason this is buggy
            #training_data = random.shuffle(training_data)

            for k in range(0,len(training_data),batch_size):
                mini_batch = training_data[k:k+batch_size]
                self.update_mini_batch(mini_batch,learning_rate)


    def update_mini_batch(self, mini_batch, learning_rate):
        biases = [np.zeros(b.shape) for b in self.biases]
        weights = [np.zeros(w.shape) for w in self.weights]

        for x,y in mini_batch:
            updated_b, updated_w = self.backpropagation(x,y)
            biases = [b+upd_b for b,upd_b in zip(biases,updated_b)]
            weights = [w+upd_w for w,upd_w in zip(weights,updated_w)]

        self.weights = [weight - (learning_rate/len(mini_batch))*upd_w \
        for weight,upd_w in zip(self.weights, weights)]

        self.biases = [bias - (learning_rate/len(mini_batch))*upd_b \
        for bias,upd_b in zip(self.biases,biases)]

    def backpropagation(self, x, y):
        nabla_b = [np.zeros(b.shape) for b in self.biases]
        nabla_w = [np.zeros(w.shape) for w in self.weights]
        # feedforward
        activation = x
        activations = [x] # list to store all the activations, layer by layer
        zs = [] # list to store all the z vectors, layer by layer
        for b, w in zip(self.biases, self.weights):
            z = np.dot(w, activation)+b
            zs.append(z)
            activation = sigmoid(z)
            activations.append(activation)
        # backward pass
        delta = (self.cost_function).delta_L(zs[-1], activations[-1], y)
        nabla_b[-1] = delta
        nabla_w[-1] = np.dot(delta, activations[-2].transpose())

        for l in range(2, self.num_layers):
            z = zs[-l]
            sp = sigmoid_derivative(z)
            delta = np.dot(self.weights[-l+1].transpose(), delta) * sp
            nabla_b[-l] = delta
            nabla_w[-l] = np.dot(delta, activations[-l-1].transpose())
        return (nabla_b, nabla_w)

def sigmoid(z):
    return 1.0/(1.0+np.exp(-z))

def sigmoid_derivative(z):
    return sigmoid(z) * (1-sigmoid(z))

    def save(self, file):
        data = {"sizes": self.sizes,
                "weights": [w.tolist() for w in self.weights],
                "biases": [b.tolist() for b in self.biases],
                "cost": str(self.cost.__name__)}
        f = open(file, "w")
        json.dump(data, f)
        f.close()

def load(file):
    f = open(file, "r")
    data = json.load(f)
    f.close()
    cost = getattr(sys.modules[__name__], data["cost"])
    net = Network(data["sizes"], cost=cost)
    net.weights = [np.array(w) for w in data["weights"]]
    net.biases = [np.array(b) for b in data["biases"]]
    return net

def vectorize(y,dim):
    res = np.zeros((dim,1))
    res[y] = 1
    return res

"""
from mnist import MNIST
mndata = MNIST('MNISTdata')
images, labels = mndata.load_training()
labels = [vectorize(label,10) for label in labels]
nn = FullyConnectedNN([784,30,10])
training_data = list(zip(images,labels))
print(nn.weights[1])
nn.SGD(training_data,1,30,1)
print(nn.weights[1])
"""


network = FullyConnectedNN([2,3,4])
"""
for i in range(len(network.weights)):
    print("weight " + str(i))
    print(network.weights[i])
    print("bias " + str(i))
    print(network.biases[i])
"""

test_data = [([1,2],7),([0,1],3)]
network.SGD(test_data,1,1,1)
when i try to run it it throws an error in line 89 and says "list object has no attribute transpose()".
I do not get why this happens since in line 80 I make the same call yet no error is raised.
Thanks for your time!
Reply
#2
please show complete unaltered error in BBcode error tags. It contains valuable debug info.
Reply
#3
Sorry about that.

Error:
Traceback (most recent call last): File "FullyConnectedNetwork.py", line 143, in <module> network.SGD(test_data,1,1,1) File "FullyConnectedNetwork.py", line 47, in SGD self.update_mini_batch(mini_batch,learning_rate) File "FullyConnectedNetwork.py", line 55, in update_mini_batch updated_b, updated_w = self.backpropagation(x,y) File "FullyConnectedNetwork.py", line 87, in backpropagation nabla_w[-l] = np.dot(delta, activations[-l-1].transpose()) AttributeError: 'list' object has no attribute 'transpose'
Reply
#4
I think you want: nabla_w[-l] = np.dot(delta, activations[-l-1]).transpose()
Reply
#5
Thanks for your answer. If I do that it throws

Error:
Traceback (most recent call last): File "FullyConnectedNetwork.py", line 143, in <module> network.SGD(test_data,1,1,1) File "FullyConnectedNetwork.py", line 47, in SGD self.update_mini_batch(mini_batch,learning_rate) File "FullyConnectedNetwork.py", line 55, in update_mini_batch updated_b, updated_w = self.backpropagation(x,y) File "FullyConnectedNetwork.py", line 87, in backpropagation nabla_w[-l] = np.dot(delta, activations[-l-1]).transpose() File "<__array_function__ internals>", line 5, in dot ValueError: shapes (3,3) and (2,) not aligned: 3 (dim 1) != 2 (dim 0)
I am on the verge of giving up...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using SHAP Library for my LSTM model - "Attribute Error" vatsalmtailor 0 2,105 Jul-13-2022, 04:34 PM
Last Post: vatsalmtailor
  Attribute Error Raiden_Arctica 4 4,167 Mar-13-2021, 11:09 AM
Last Post: Raiden_Arctica
  Attribute error in Dataframes deshanish 1 2,423 Jul-15-2020, 08:38 AM
Last Post: snippsat
  Attribute error - Sympy VictorG8 1 5,013 Apr-09-2020, 06:03 PM
Last Post: snippsat
  Series object error message abhaydd 1 4,909 Aug-11-2019, 01:29 AM
Last Post: boring_accountant
  Pandas to_csv in for loop AttributeError: 'tuple' object has no attribute 'to_csv' NSearch 9 16,940 Apr-22-2019, 05:05 PM
Last Post: Yoriz
  AttributeError: 'NoneType' object has no attribute 'all' synthex 2 5,314 Mar-07-2019, 11:11 AM
Last Post: synthex
  Please help with AttributeError: 'Netz' object has no attribute 'conv' DerBerliner 2 3,804 Feb-27-2019, 06:01 PM
Last Post: DerBerliner
  NLTK Download Attribute error laila1a 1 8,616 Jan-27-2019, 12:03 AM
Last Post: Larz60+
  'list' object has no attribute 'reshape' SamSoftwareLtd 1 15,622 Nov-04-2018, 10:38 PM
Last Post: stullis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020