Ask Your Question

Revision history [back]

TypeError: 'int' object is not iterable

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.

click to hide/show revision 2
retagged

TypeError: 'int' object is not iterable

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.