SynonymFilter

import tensorflow as tf
import numpy as np
import scipy.io as io
import sys
import os




# Define the flags useable from the command line.
tf.app.flags.DEFINE_string('train', None,
                           'File containing the training data (labels & features).')
tf.app.flags.DEFINE_integer('num_epochs', 50,
                            'Number of training epochs.')
tf.app.flags.DEFINE_integer('batch_size', 2000,
                            'Batch Size.')


FLAGS = tf.app.flags.FLAGS


# Extract numpy representations of the labels and features given rows consisting of:
#   label, feat_0, feat_1, ..., feat_n
#   The given file should be a comma-separated-values (CSV) file saved by the savetxt command.
def extract_data(filename):
    
    out = np.loadtxt(filename, dtype=np.str,delimiter="\t",comments=None);
    np.random.shuffle(out)
    labels = out[:,2]
    fvecs = out[:,3:]
    # Arrays to hold the labels and feature vectors.
    labels = labels.astype(np.int32)
    labels = labels.reshape(labels.size,1)


    print "raw shape=",fvecs.shape
    fvecs = fvecs.astype(np.float).reshape((fvecs.shape[0], 787))
    
    print labels.shape
    print fvecs.shape
    # Return a pair of the feature matrix and the one-hot label matrix.
    return fvecs,labels




def product(X,w):
    c = tf.einsum('ijl,lk->ijk', X, w)
    return c


def main(argv=None):


    # Get the data.
    train_data_filename = FLAGS.train
    # Extract it into numpy matrices.
    X_raw,y_raw = extract_data(train_data_filename)
 
    total_size = X_raw.shape[0]
    
    train_size = int(total_size*9/10)
    test_size = int(total_size*1/10)
    


    X_train = X_raw[:train_size,:]
    y_train = y_raw[:train_size,:]


    X_test = X_raw[train_size:train_size+test_size,:]
    y_test = y_raw[train_size:train_size+test_size,:]
    # Get the shape of the training data.
    




    # Get the number of epochs for training.
    BATCH_SIZE = FLAGS.batch_size
    num_epochs = FLAGS.num_epochs


    
    #layer 0
    X_place = tf.placeholder("float", shape=[None, 787])
    y_place = tf.placeholder("float", shape=[None, 1])




    #layer 1
    low=-np.sqrt(6. / (787 + 400))
    high=np.sqrt(6. / (787 + 400))
    layer1_W = tf.Variable(tf.random_uniform([787,400], low, high), name='weight1')
    layer1_b = tf.Variable(tf.constant(0.1,shape=[400]),dtype = tf.float32,name='bias1')
    layer1_z = tf.matmul(X_place,layer1_W) + layer1_b
    layer1_y = tf.nn.tanh(layer1_z)
    print "layer1_y",layer1_y.shape
    
    low=-np.sqrt(6. / (400 + 200))
    high=np.sqrt(6. / (400 + 200))
    layer2_W = tf.Variable(tf.random_uniform([400,200], low, high), name='weight2')
    layer2_b = tf.Variable(tf.constant(0.1,shape=[200]),dtype = tf.float32,name='bias2')
    layer2_z = tf.matmul(layer1_y,layer2_W) + layer2_b
    layer2_y = tf.nn.tanh(layer2_z)
    print "layer2_y",layer2_y.shape
    
     #layer 5
    low=-np.sqrt(6. / (200 + 1))
    high=np.sqrt(6. / (200 + 1))
    layer3_W = tf.Variable(tf.random_uniform([200,1], low, high), name='weight3')
    layer3_b = tf.Variable(tf.constant(0.1,shape=[1]),dtype = tf.float32,name='bias3')
    layer3_z = tf.matmul(layer2_y,layer3_W) + layer3_b
    layer3_y = layer3_z
    print "layer3_y",layer3_y.shape
    




    # Define and initialize the network.


    # These are the weights that inform how much each feature contributes to


    # Optimization.
    error = layer3_y - y_place    
    #fake = tf.cast(error*y_place<0,tf.float32)
    loss = tf.sqrt(tf.reduce_mean(tf.square(error)))
    train_step = tf.train.AdamOptimizer(0.01).minimize(loss)


    # Evaluation.
    
    '''
    correct_prediction = (layer4_y*y_place > 0)
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
    '''
    
    


    # Create a local session to run this computation.
    with tf.Session() as sess:
        # Run all the initializers to prepare the trainable parameters.
        tf.initialize_all_variables().run()


        # Iterate and train.
        for epoch in xrange(num_epochs):


            randomize = np.arange(train_size)
            np.random.shuffle(randomize)
            X_train = X_train[randomize]
            y_train = y_train[randomize]


    
            for step in xrange(train_size // BATCH_SIZE):
                offset = (step * BATCH_SIZE) % train_size
                batch_data = X_train[offset:(offset + BATCH_SIZE), :]
                batch_labels = y_train[offset:(offset + BATCH_SIZE)]
                train_step.run(feed_dict={X_place: batch_data, y_place: batch_labels})
    
                print 'epoch:%d step:%d,rmse: %.4f' \
                    %(epoch,step, \
                    loss.eval(feed_dict={X_place: batch_data, y_place: batch_labels}))
    
            # Give very detailed output.
            print "Epoch:%d,rmse: %f on test: " \
                    %(epoch, \
                    loss.eval(feed_dict={X_place: X_test, y_place: y_test}))
    


    
if __name__ == '__main__':
    tf.app.run()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值