<jupyter_start><jupyter_text>Build Model<jupyter_code>import tensorflow as tf
from Expression_Network import Expression_Network
num_classes = len(count.most_common())
x = tf.placeholder(dtype=tf.float32, shape=(None,128,128),name='x')
y = tf.placeholder(dtype=tf.int32, shape=(None),name="y")
lr = tf.placeholder(dtype=tf.float32, shape=[], name="lr")
one_hot = tf.one_hot(y,num_classes)
reshaped = tf.expand_dims(x,-1)

exnet = Expression_Network(num_classes,Training=True)
logits,prediction = exnet.model(x)
cross_entropy = tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits, labels=one_hot)
loss_op = tf.reduce_mean(cross_entropy)
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
with tf.control_dependencies(update_ops):
    optimizer = tf.train.AdamOptimizer(lr).minimize(loss_op)
correct_pred = tf.equal(tf.argmax(prediction,1), tf.argmax(one_hot,1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32), name='accuracy')
epochs = 150
learn_rate = .0001
batch_size = 64

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    saver = tf.train.Saver()
    for epoch in range(epochs):
        batch_i = 1
        
        for x_batch,y_batch in next_batch(x_train,y_train,batch_size):
            sess.run(optimizer,feed_dict={x:x_batch,y:y_batch,lr:learn_rate,exnet.train_pl:True})
        
            if batch_i % 10 == 0:
            
                valacc = sess.run(accuracy,feed_dict={x:x_val,y:y_val,exnet.train_pl:False})
                loss, trainacc = sess.run([loss_op, accuracy], feed_dict={x:x_train,y:y_train,exnet.train_pl:False})
                print("epoch:{} batch:{}\ttrain accuracy: {}\tloss: {}\tval acc: {}"
                 .format(epoch,batch_i,round(float(trainacc),4),round(float(loss),4), round(float(valacc),4)))
                
                
            batch_i += 1
    X,Y = balanced_subset(images,nplabels)
    X,Y = shuffle(X,Y)
    #print(sess.run(prediction,feed_dict={x:X,y:Y}))
    
  
    save_path = saver.save(sess, "checkpoints/model.ckpt")

            
batch_i = np.array(0)
trainacc = np.array(0.238473172372)
loss = np.array(12314.52365646352)
print("batch: {}\ttrain accuracy: {}\tloss: {}"
                      .format(batch_i,np.round(trainacc,4),np.round(loss,4)))
num_classes = len(count.most_common())
x = tf.placeholder(dtype=tf.float64, shape=(None,128,128),name='x')
y = tf.placeholder(dtype=tf.int32, shape=(None),name="y")
lr = tf.placeholder(dtype=tf.float32, shape=[], name="lr")
#batch,128,128,1

#first convolution
conv = tf.contrib.layers.conv2d(
    reshaped,
    32,
    (9,9),
    stride=(1,1),
    padding='VALID',
    weights_initializer=tf.initializers.random_normal)
#batch,120,120,32

#subsample
pool = tf.contrib.layers.max_pool2d(
    conv,
    (4,4),
    stride=(4,4))
#batch,30,30,32

#second convolution
conv2 = tf.contrib.layers.conv2d(
    pool,
    64,
    (11,11),
    stride=(1,1),
    padding='VALID',
    weights_initializer=tf.initializers.random_normal)
#batch,20,20,64

#subsample
pool2 = tf.contrib.layers.max_pool2d(
    conv2,
    (4,4),
    stride=(4,4))
#batch,5,5,64

#flatten
flat = tf.contrib.layers.flatten(pool2)
#batch,1600

#fully connected
fc = tf.contrib.layers.fully_connected(
    flat,
    256,
    activation_fn=tf.nn.leaky_relu)

#batch,256
logits = tf.contrib.layers.fully_connected(
    fc,
    num_classes,
    activation_fn=None)

prediction = tf.nn.softmax(logits,)
with tf.Session() as sess:
    saver = tf.train.Saver()
    saver.restore(sess, "checkpoints/model.ckpt")
    sess.run()<jupyter_output>INFO:tensorflow:Restoring parameters from checkpoints/model.ckpt
StarcoderdataJupyterStructuredCleanDedup