Unknown Shin
Unknown Shin

Reputation: 119

how to append different return values in a list

I have a function something like:

def function(self):
    matrix = []
    for i in range(10):
        contents = [1,0,1,0] #integer values in a list, changes every time when running function()
        matrix .append(contents)
    self.matrix = matrix 
    return matrix 

and I have another empty list, what I’m trying to do is to append matrix into empty_matrix 5 times, So the result should be like:

empty_matrix = [[matrix1],[matrix2],[matrix3],[matrix4],[matrix5]]

But I’m not sure how to make Matrix1,2,3,4,5 have different values. When I run function(self) alone it returns different value, but when I append it into empty_matrix, it appends the same value, like

empty_matrix = [[matrix1],[matrix1],[matrix1],[matrix1],[matrix1]]. 

Any helps would be appreciated.

Original code is attached below:

def generate_population(self):
    model = self.model
    weights_origin = model.get_weights()
    shape_list = []
    matrix= []
    empty_matrix= [[],[],[],[],[]]


    for i, w in enumerate(weights_origin):
        #get shape of w -> save shape into shape_list -> generate binary_value that has the shape of shape_list 
        #-> convert binary_value to float32 -> define it as tf Variable -> append to rand_covers list
        w_shape = tf.shape(w)
        shape_list.append(w_shape)
        binary_value = np.random.randint(2, size = shape_list[i])      
        to_float = tf.cast(binary_value, tf.float32)  
        weights2 = tf.Variable(to_float, name="addition_"+str(i), trainable=True)
        matrix.append(weights2)

    for j in range(5):
        empty_matrix[j].append(matrix)


self.matrix = matrix
return matrix

Upvotes: 0

Views: 52

Answers (1)

furas
furas

Reputation: 142641

Instead of

for j in range(5):
    empty_matrix[j].append(matrix)

you need

for j in range(5):
    matrix = function()
    empty_matrix[j].append(matrix)

If you want to use the same matrix at start and later change values in one of them without changing values in others then you have to duplicate them using .copy()

matrix = function()

for j in range(5):
    empty_matrix[j].append( matrix.copy() )

or copy.deepcopy() for deep duplication

import copy

matrix = function()

for j in range(5):
    empty_matrix[j].append( copy.deepcopy(matrix.copy) )

EDIT:

I don't understand what you try to do but code seems strange and I would write it

def generate_population(self):
    model = self.model
    weights_origin = model.get_weights()
    shape_list = []

    empty_matrix= [[],[],[],[],[]]

    for j in range(5):

        matrix = []

        for i, w in enumerate(weights_origin):
            #get shape of w -> save shape into shape_list -> generate binary_value that has the shape of shape_list 
            #-> convert binary_value to float32 -> define it as tf Variable -> append to rand_covers list
            w_shape = tf.shape(w)
            shape_list.append(w_shape)
            binary_value = np.random.randint(2, size = shape_list[i])      
            to_float = tf.cast(binary_value, tf.float32)  
            weights2 = tf.Variable(to_float, name="addition_"+str(i), trainable=True)
            matrix.append(weights2)

        empty_matrix[j].append(matrix)

    return empty_matrix

# --- outside function ---

empty_matrix = generate_population()

or

def generate_population(self):
    model = self.model
    weights_origin = model.get_weights()
    shape_list = []

    matrix = []

    for i, w in enumerate(weights_origin):
        #get shape of w -> save shape into shape_list -> generate binary_value that has the shape of shape_list 
        #-> convert binary_value to float32 -> define it as tf Variable -> append to rand_covers list
        w_shape = tf.shape(w)
        shape_list.append(w_shape)
        binary_value = np.random.randint(2, size=shape_list[i])      
        to_float = tf.cast(binary_value, tf.float32)  
        weights2 = tf.Variable(to_float, name="addition_"+str(i), trainable=True)
        matrix.append(weights2)

    return matrix

# --- outside function ---

empty_matrix= [[],[],[],[],[]]

for j in range(5):
    matrix = generate_population()
    empty_matrix[j].append(matrix)

Upvotes: 1

Related Questions