How to verify is an Neural Network was trained ?

by: elias.temponi, 7 years ago


Hi, I am still learning deep machine.
I am trying to use one of your example  and converting to my own data.

The problem is: I dont think my model was trained ... a got accuray of 100% and losses =0 for every epoch.

Here is my code.

import tensorflow as tf

train_x = [[ 2.3004, 0 , 19.58 ,0.605 , 6.319, 96.1  , 2.1   , 403, 14.7],
           [13.3598, 0 , 18.1  ,0.693 , 5.887, 94.7  , 1.7821, 666, 20.2],
           [0.12744, 0 , 6.91  ,0.448 , 6.770, 2.90  , 5.7209, 233, 17.9],
           [0.15876, 0 , 10.81 ,0.413 , 5.961, 17.50 , 5.2873, 305, 19.2],
           [0.03768, 80, 1.52  ,0.404 , 7.274, 38.30 , 7.309 , 329, 12.6],
           [0.03705, 20, 3.33  ,0.442 , 6.968, 37.20 , 5.2447, 216, 14.9],
           [0.07244, 60, 1.69  ,0.411 , 5.884, 18.50 , 10.710, 411, 18.3],
           [0.1    , 34, 6.09  ,0.433 , 6.982, 17.70 , 5.4917, 329, 16.1]
          ]
train_y = [[23.8],
           [12.7],
           [26.6],
           [21.7],
           [34.6],
           [35.4],
           [18.6],
           [33.1]
          ]

test_x = [[ 11.9511,0.0,18.1,0.659,5.608,100.0,1.2852,666,20.2],
           [22.0511,0.0,18.1,0.74,5.818,92.4,1.8662,666,20.2],
           [0.19073,22.0,5.86,0.431,6.718,17.5,7.8265,330,19.1],
           [4.64689,0.0,18.1,0.614,6.98,67.6,2.5329,666,20.2],
          ]
test_y = [ [27.9],
           [10.5],
           [26.2],
           [29.8]
         ]

prediction_x = [[7.83932,0.0,18.1,0.655,6.209,65.4,2.9634,666,20.2]]
          
          
n_nodes_hl1 = 3
n_nodes_hl2 = 3
n_nodes_hl3 = 3

n_classes = 1
batch_size = 8
hm_epochs = 10


x = tf.placeholder('float',[None, len(train_x[0])])
y = tf.placeholder('float')

hidden_1_layer = {'f_fum':n_nodes_hl1,
                  'weight':tf.Variable(tf.random_normal([len(train_x[0]), n_nodes_hl1])),
                  'bias':tf.Variable(tf.random_normal([n_nodes_hl1]))}

hidden_2_layer = {'f_fum':n_nodes_hl2,
                  'weight':tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])),
                  'bias':tf.Variable(tf.random_normal([n_nodes_hl2]))}

hidden_3_layer = {'f_fum':n_nodes_hl3,
                  'weight':tf.Variable(tf.random_normal([n_nodes_hl2, n_nodes_hl3])),
                  'bias':tf.Variable(tf.random_normal([n_nodes_hl3]))}

output_layer = {'f_fum':None,
                'weight':tf.Variable(tf.random_normal([n_nodes_hl3, n_classes])),
                'bias':tf.Variable(tf.random_normal([n_classes])),}


# Nothing changes
def neural_network_model(data):

    l1 = tf.add(tf.matmul(data,hidden_1_layer['weight']), hidden_1_layer['bias'])
    l1 = tf.nn.relu(l1)

    l2 = tf.add(tf.matmul(l1,hidden_2_layer['weight']), hidden_2_layer['bias'])
    l2 = tf.nn.relu(l2)

    l3 = tf.add(tf.matmul(l2,hidden_3_layer['weight']), hidden_3_layer['bias'])
    l3 = tf.nn.relu(l3)

    output = tf.matmul(l3,output_layer['weight']) + output_layer['bias']

    return output


def train_neural_network(x):
    prediction = neural_network_model(x)
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y))
    optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)
    
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
    
        for epoch in range(hm_epochs):
            epoch_loss = 0
            i=0
            while i < len(train_x):
                start = i
                end = i+batch_size
                batch_x = np.array(train_x[start:end])
                batch_y = np.array(train_y[start:end])

                _, c = sess.run([optimizer, cost], feed_dict={x: batch_x, y: batch_y})
                epoch_loss += c
                i+=batch_size
                
            print('Epoch', epoch+1, 'completed out of',hm_epochs,'loss:',epoch_loss)
        correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
        accuracy = tf.reduce_mean(tf.cast(correct, 'float'))

        print('Accuracy:',accuracy.eval({x:test_x, y:test_y}))
  
def use_neural_network():
    prediction = neural_network_model(x)
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        result = (sess.run(tf.argmax(prediction.eval(feed_dict={x:[prediction_x[0]]}),1)))
        print (result)

train_neural_network(x)
use_neural_network()



You must be logged in to post. Please login or register an account.




import tensorflow as tf

train_x = [[ 2.3004, 0 , 19.58 ,0.605 , 6.319, 96.1  , 2.1   , 403, 14.7],
           [13.3598, 0 , 18.1  ,0.693 , 5.887, 94.7  , 1.7821, 666, 20.2],
           [0.12744, 0 , 6.91  ,0.448 , 6.770, 2.90  , 5.7209, 233, 17.9],
           [0.15876, 0 , 10.81 ,0.413 , 5.961, 17.50 , 5.2873, 305, 19.2],
           [0.03768, 80, 1.52  ,0.404 , 7.274, 38.30 , 7.309 , 329, 12.6],
           [0.03705, 20, 3.33  ,0.442 , 6.968, 37.20 , 5.2447, 216, 14.9],
           [0.07244, 60, 1.69  ,0.411 , 5.884, 18.50 , 10.710, 411, 18.3],
           [0.1    , 34, 6.09  ,0.433 , 6.982, 17.70 , 5.4917, 329, 16.1]
          ]
train_y = [[23.8],
           [12.7],
           [26.6],
           [21.7],
           [34.6],
           [35.4],
           [18.6],
           [33.1]
          ]

test_x = [[ 11.9511,0.0,18.1,0.659,5.608,100.0,1.2852,666,20.2],
           [22.0511,0.0,18.1,0.74,5.818,92.4,1.8662,666,20.2],
           [0.19073,22.0,5.86,0.431,6.718,17.5,7.8265,330,19.1],
           [4.64689,0.0,18.1,0.614,6.98,67.6,2.5329,666,20.2],
          ]
test_y = [ [27.9],
           [10.5],
           [26.2],
           [29.8]
         ]

prediction_x = [[7.83932,0.0,18.1,0.655,6.209,65.4,2.9634,666,20.2]]
          
          
n_nodes_hl1 = 3
n_nodes_hl2 = 3
n_nodes_hl3 = 3

n_classes = 1
batch_size = 8
hm_epochs = 10


x = tf.placeholder('float',[None, len(train_x[0])])
y = tf.placeholder('float')

hidden_1_layer = {'f_fum':n_nodes_hl1,
                  'weight':tf.Variable(tf.random_normal([len(train_x[0]), n_nodes_hl1])),
                  'bias':tf.Variable(tf.random_normal([n_nodes_hl1]))}

hidden_2_layer = {'f_fum':n_nodes_hl2,
                  'weight':tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])),
                  'bias':tf.Variable(tf.random_normal([n_nodes_hl2]))}

hidden_3_layer = {'f_fum':n_nodes_hl3,
                  'weight':tf.Variable(tf.random_normal([n_nodes_hl2, n_nodes_hl3])),
                  'bias':tf.Variable(tf.random_normal([n_nodes_hl3]))}

output_layer = {'f_fum':None,
                'weight':tf.Variable(tf.random_normal([n_nodes_hl3, n_classes])),
                'bias':tf.Variable(tf.random_normal([n_classes])),}


# Nothing changes
def neural_network_model(data):

    l1 = tf.add(tf.matmul(data,hidden_1_layer['weight']), hidden_1_layer['bias'])
    l1 = tf.nn.relu(l1)

    l2 = tf.add(tf.matmul(l1,hidden_2_layer['weight']), hidden_2_layer['bias'])
    l2 = tf.nn.relu(l2)

    l3 = tf.add(tf.matmul(l2,hidden_3_layer['weight']), hidden_3_layer['bias'])
    l3 = tf.nn.relu(l3)

    output = tf.matmul(l3,output_layer['weight']) + output_layer['bias']

    return output


def train_neural_network(x):
    prediction = neural_network_model(x)
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y))
    optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)
    
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
    
        for epoch in range(hm_epochs):
            epoch_loss = 0
            i=0
            while i < len(train_x):
                start = i
                end = i+batch_size
                batch_x = np.array(train_x[start:end])
                batch_y = np.array(train_y[start:end])

                _, c = sess.run([optimizer, cost], feed_dict={x: batch_x, y: batch_y})
                epoch_loss += c
                i+=batch_size
                
            print('Epoch', epoch+1, 'completed out of',hm_epochs,'loss:',epoch_loss)
        correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
        accuracy = tf.reduce_mean(tf.cast(correct, 'float'))

        print('Accuracy:',accuracy.eval({x:test_x, y:test_y}))
  
def use_neural_network():
    prediction = neural_network_model(x)
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        result = (sess.run(tf.argmax(prediction.eval(feed_dict={x:[prediction_x[0]]}),1)))
        print (result)

train_neural_network(x)
use_neural_network()


-elias.temponi 7 years ago

You must be logged in to post. Please login or register an account.