将文件名放在哪里以及如何在此tensorflow代码中放置文件名? [英] where and how to put the filename in this tensorflow code?

查看:103
本文介绍了将文件名放在哪里以及如何在此tensorflow代码中放置文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从Sirajology的github复制了一个tensorflow代码.它应将.csv加载到单层神经网络中.

I've copied a tensorflow code from Sirajology's github. It should load a .csv into a single layer neural net.

我的问题是,如何将.csv文件放入代码中?

My question is, how and where do I put the .csv file into the code?

此外,我不知道代码是否会自动将.csv拆分为训练数据和测试数据,还是在将其输入到神经网络之前是否需要使用一些其他代码来做到这一点?

Also, I don't understand if the code will automatically split the .csv into train and test data, or if I need to do that with some different code before it is fed to the neural net?

我已经在python和tensorflow上花费了很多时间,并且了解了一些基本概念,但是仍然是个新手.任何帮助表示赞赏!谢谢!!!

I've been spending a lot of time with python and tensorflow and understand some basic concepts but am still a total newb. Any help is appreciated! Thanks!!!

#I have eliminated all code that is obviously irrelevant to the question

tf.app.flags.DEFINE_string('train', None,
                       'File containing the training data (labels & features).')
tf.app.flags.DEFINE_string('test', None,
                       'File containing the test data (labels & features).')
tf.app.flags.DEFINE_integer('num_epochs', 1,
                        'Number of examples to separate from the training '
                        'data for the validation set.')
tf.app.flags.DEFINE_boolean('verbose', False, 'Produce verbose output.')
FLAGS = tf.app.flags.FLAGS

# Extract numpy representations of the labels and features given rows consisting of:
#   label, feat_0, feat_1, ..., feat_n
def extract_data(filename):

# Arrays to hold the labels and feature vectors.
    labels = []
    fvecs = []

# Iterate over the rows, splitting the label from the features. Convert labels
# to integers and features to floats.
for line in file(filename):
    row = line.split(",")
    labels.append(int(row[0]))
    fvecs.append([float(x) for x in row[1:]])

# Convert the array of float arrays into a numpy float matrix.
fvecs_np = np.matrix(fvecs).astype(np.float32)

# Convert the array of int labels into a numpy array.
labels_np = np.array(labels).astype(dtype=np.uint8)

# Convert the int numpy array into a one-hot matrix.
labels_onehot = (np.arange(NUM_LABELS) == labels_np[:, None]).astype(np.float32)

# Return a pair of the feature matrix and the one-hot label matrix.
return fvecs_np,labels_onehot

def main(argv=None):
# Be verbose?
verbose = FLAGS.verbose

# Get the data.
train_data_filename = FLAGS.train
test_data_filename = FLAGS.test

# Extract it into numpy matrices.
train_data,train_labels = extract_data(train_data_filename)
test_data, test_labels = extract_data(test_data_filename)

# Get the shape of the training data.
train_size,num_features = train_data.shape

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

# This is where training samples and labels are fed to the graph.
# These placeholder nodes will be fed a batch of training data at each
# training step using the {feed_dict} argument to the Run() call below.
x = tf.placeholder("float", shape=[None, num_features])
y_ = tf.placeholder("float", shape=[None, NUM_LABELS])

# For the test data, hold the entire dataset in one constant node.
test_data_node = tf.constant(test_data)

# Define and initialize the network.

# These are the weights that inform how much each feature contributes to
# the classification.
W = tf.Variable(tf.zeros([num_features,NUM_LABELS]))
b = tf.Variable(tf.zeros([NUM_LABELS]))
y = tf.nn.softmax(tf.matmul(x,W) + b)

# Optimization.
cross_entropy = -tf.reduce_sum(y_*tf.log(y))
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)

# Evaluation.
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

# Create a local session to run this computation.
with tf.Session() as s:
    # Run all the initializers to prepare the trainable parameters.
    tf.initialize_all_variables().run()
    if verbose:
        print ('Initialized!')
        print
        print ('Training.')

    # Iterate and train.
    for step in xrange(num_epochs * train_size // BATCH_SIZE):
        if verbose:
            print (step,)

        offset = (step * BATCH_SIZE) % train_size
        batch_data = train_data[offset:(offset + BATCH_SIZE), :]
        batch_labels = train_labels[offset:(offset + BATCH_SIZE)]
        train_step.run(feed_dict={x: batch_data, y_: batch_labels})

        if verbose and offset >= train_size-BATCH_SIZE:
            print

    # Give very detailed output.
    if verbose:
        print
        print ('Weight matrix.')
        print (s.run(W))
        print
        print ('Bias vector.')
        print (s.run(b))
        print
        print ("Applying model to first test instance.")
        first = test_data[:1]
        print ("Point =", first)
        print ("Wx+b = ", s.run(tf.matmul(first,W)+b))
        print ("softmax(Wx+b) = ", s.run(tf.nn.softmax(tf.matmul(first,W)+b)))
        print

    print ("Accuracy:", accuracy.eval(feed_dict={x: test_data, y_: test_labels}))


if __name__ == '__main__':
tf.app.run()

推荐答案

期望在终端中将其作为参数接收.

It's expecting to receive it as argument in the terminal.

下面的行正在检查它:

tf.app.flags.DEFINE_string('train', None,
                       'File containing the training data (labels & features).')
tf.app.flags.DEFINE_string('test', None,
                       'File containing the test data (labels & features).')
tf.app.flags.DEFINE_integer('num_epochs', 1,
                        'Number of examples to separate from the training '
                        'data for the validation set.')

因此,您只需要以以下方式运行它:

So, you just have to run it as:

python YourScript.py --train FileName.csv --test TestName.csv --num_epochs 5 --verbose True

这篇关于将文件名放在哪里以及如何在此tensorflow代码中放置文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆