我有以下问题。我希望在文本文件中导出数据。但我确实需要在循环中添加一些标题。例如:
#!/usr/bin/env python
import tensorflow as tf
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data
def init_weights(shape):
return tf.Variable(tf.random_normal(shape, stddev=0.01))
def model(X, w_h, w_o):
h = tf.nn.sigmoid(tf.matmul(X, w_h)) # this is a basic mlp, think 2 stacked logistic regressions
return tf.matmul(h, w_o) # note that we dont take the softmax at the end because our cost fn does that for us
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
trX, trY, teX, teY = mnist.train.images, mnist.train.labels, mnist.test.images, mnist.test.labels
X = tf.placeholder("float", [None, 784])
Y = tf.placeholder("float", [None, 10])
w_h = init_weights([784, 625]) # create symbolic variables
w_o = init_weights([625, 10])
py_x = model(X, w_h, w_o)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=py_x, labels=Y)) # compute costs
train_op = tf.train.GradientDescentOptimizer(0.05).minimize(cost) # construct an optimizer
predict_op = tf.argmax(py_x, 1)
# Launch the graph in a session
with tf.Session() as sess:
# you need to initialize all variables
tf.global_variables_initializer().run()
for i in range(100):
for start, end in zip(range(0, len(trX), 128), range(128, len(trX)+1, 128)):
sess.run(train_op, feed_dict={X: trX[start:end], Y: trY[start:end]})
print(i, np.mean(np.argmax(teY, axis=1) ==
sess.run(predict_op, feed_dict={X: teX})))
"我"在标题中很重要,它表示分析阶段,对我的其他软件使用至关重要。它必须放在文本之间。此外,标题中的文字不应该包含那些" "去除它。任何的想法?谢谢!
起初,我使用Tcl来完成这项工作,但数据太大(超过2G),所以Tcl不会这样做。
答案 0 :(得分:1)
write.table(粘贴(" SomeText - ",i," - 某些文字"))
答案 1 :(得分:1)
我想了解你的意思,对不起如果你没有找到合适的答案。我想让你为每一行添加一个带有一些ID的列。 在我看来,你可以避免for循环。 这是一个工作实例
data(iris)
df<-iris
head(df)
nrow=dim(df)[1]
i<-1:nrow
df<-data.frame(id=paste("SomeText --", i ,"-- some text ",sep=""),df)
head(df)
write.table(df,file="file.csv",sep=",",row.names=FALSE,quote=FALSE)
quote = FALSE阻止R在字符串字段中添加“”