0

This is a layer in tensorflow

def eachLayer(inputX,numberOfHiddenInputs,name,activation=tf.nn.relu):
    with tf.variable_scope(name):
        init = tf.random_normal(shape=(int(inputX.get_shape()[1]),numberOfHiddenInputs))        
        weights = tf.Variable(init,dtype="float32",name="weights")
        biases = tf.Variable(tf.zeros([numberOfHiddenInputs]),dtype='float32',name="biases")
        output=tf.matmul(inputX,weights) + biases
        if activation:
            return activation(output)
        else:
            return output

The entire network is created in this function as

def DNN(X=X,reuse=False):
    with tf.variable_scope("dnn"):
        first_layer = eachLayer(X,hidden_,name="firstLayer")
        second_layer = eachLayer(first_layer,hidden_,name="secondLayer")
        third_layer = eachLayer(second_layer,hidden_,name="thirdLayer")
        output = eachLayer(third_layer,outputSize,name="output",activation=None)
        return output

How do I save the "dnn" in session, which can be later used for testing purpose? I have tried saving the session, but it can only restore the weights, and biases and I don't want to build the entire model again to test any image.

with tf.Session() as sess:
    saver = tf.train.Saver()
    global_init = tf.global_variables_initializer()
    sess.run(global_init)
    for _ in range(epoch):
        k=0
        for eachBatch in range(noOfBatch):
            batch_xs,batch_ys = x_train[k:k+batchSize], y_train[k:k+batchSize]
            nth= sess.run(min_loss,feed_dict={X:batch_xs,Y:batch_ys})
            theMinLossVal= mse.eval(feed_dict={X:batch_xs,Y:batch_ys})
            k=k+batchSize         
        print("THE MIN LOSS IS ==> {}".format(theMinLossVal))
    saver.save(sess, "mnistModel.ckpt")

    print("Model Saved")
JustABeginner
  • 785
  • 2
  • 11
  • 26

0 Answers0