Ask Your Question
0

TypeError: 'int' object is not iterable

asked 2018-01-11 05:16:19 +0200

updated 2018-01-17 09:20:53 +0200

FrédéricC gravatar image

I'm trying to generate an array of matrix coefficients from 0.1 to 1.0 at 0.1 interval for training neural networks. I wrote the following codes:

from __future__ import print_function
# Import MNIST data
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)
import time
import numpy as np
import tensorflow as tf
import math
import matplotlib.pyplot as plt

n_1 = 100               # 1st layer number of neurons
n_2 = 100               # 2nd layer number of neurons
n_input = 784           # MNIST data input (img shape: 28*28)
n_classes = 10

tf.reset_default_graph()
X = tf.placeholder(tf.float32, [None, n_input], name = 'X')
Y = tf.placeholder(tf.float32, [None, n_classes], name = 'Y')

def initialize_param(n_input, n_1, n_2, n_class):
    tf.set_random_seed(1)
    W1 = tf.get_variable("W1", shape = [n_input, n_1], initializer = tf.contrib.layers.xavier_initializer(seed = 1))
    b1 = tf.get_variable("b1", shape = [n_1, 1], initializer = tf.zeros_initializer())
    W2 = tf.get_variable("W2", shape = [n_1, n_2], initializer = tf.contrib.layers.xavier_initializer(seed = 1))
    b2 = tf.get_variable("b2", shape = [n_2, 1], initializer = tf.zeros_initializer())
    W3 = tf.get_variable("W3", shape = [n_2, n_class], initializer = tf.contrib.layers.xavier_initializer(seed = 1))
    b3 = tf.get_variable("b3", [n_class, 1], initializer = tf.zeros_initializer())
    parameters = {"W1": W1, 
                  "b1": b1, 
                  "W2": W2,  
                  "b2": b2, 
                  "W3": W3,
                  "b3": b3}

    return parameters
parameters = initialize_param(784, 100, 100, 10)

pc = np.linspace(0.1, 1.0, 10) 
def initialize_profile_1(pc, n_1):
    p_a11 = tf.constant(1.0, shape = [1, int(np.round(pc * (n_1)))])
    p_a12 = tf.zeros(shape = [1, int(np.round((1.0 - pc) * (n_1)))])
    prof_L1 = tf.concat((p_a11, p_a12), axis = 1)
    return prof_L1
profile_1 = []
for i in pc:
    prof_L1 = initialize_profile_1(i, 100)
    profile_L1 = tf.stack(prof_L1, axis = 0)
    profile_1.append(profile_L1) 

def initialize_profile_2(pc, n_2):           
    p_a21 = tf.constant(1.0, shape = [1, int(np.round(pc * (n_2)))])
    p_a22 = tf.zeros(shape = [1, int(np.round((1.0 - pc) * (n_2)))])
    prof_L2 = tf.concat((p_a21, p_a22), axis = 1)
    return prof_L2
profile_2 = []
for j in pc:
    prof_L2 = initialize_profile_2(j, 100)
    profile_L2 = tf.stack(prof_L2, axis = 0)
    profile_2.append(profile_L2) 

def multilayer_perceptron(x): 
    for k in len(pc):
        Z1 = tf.add(tf.matmul(x, parameters['W1']),  parameters['b1'])   
        A1 = tf.nn.relu(Z1)
        P_1 = tf.multiply(profile_1[k], A1)
        Z2 = tf.add(tf.matmul(P_1,  parameters['W2']),  parameters['b2']) 
        A2 = tf.nn.relu(Z2)
        P_2 = tf.multiply(profile_2[k], A2)
        cache = {P_1:p_a1, p_2:p_a2}
        print(cache)
multilayer_perceptron(X)

I have been getting the error: "TypeError: 'int' object is not iterable", please help me.

edit retag flag offensive close merge delete

Comments

Please try to pinpoint your error and give us a minimal example.

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2018-01-11 07:30:31 +0200 )edit
1

I can't see any Sage here. It looks like this is just Python and TensorFlow. This site is a Q&A forum for SageMath.

Simon Willerton gravatar imageSimon Willerton ( 2018-01-11 10:13:50 +0200 )edit

1 Answer

Sort by » oldest newest most voted
1

answered 2018-01-11 10:18:17 +0200

eric_g gravatar image

updated 2018-01-11 10:20:23 +0200

The error lies in the first line of multilayer_perceptron:

for k in len(pc):

Indeed, len(pc) is an integer (the length of pc), so you cannot iterate on it. Maybe you mean

for k in pc:

instead or perhaps

for k in range(len(pc)):

(depending on the nature of k and pc).

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2018-01-11 05:16:19 +0200

Seen: 2,748 times

Last updated: Jan 11 '18