我正在为初学者查看Tensorflow MNIST示例,并发现在此部分中:
for i in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
Run Code Online (Sandbox Code Playgroud)
将批量大小从100更改为高于204会导致模型无法收敛.它可以达到204,但是在205和我试过的任何更高的数字,精度最终将<10%.这是一个错误,关于算法的东西,还有什么?
这是运行OS X的二进制安装,似乎是0.5.0版本.
鉴于我有一个线性模型如下,我想得到关于W和b的梯度向量.
# tf Graph Input
X = tf.placeholder("float")
Y = tf.placeholder("float")
# Set model weights
W = tf.Variable(rng.randn(), name="weight")
b = tf.Variable(rng.randn(), name="bias")
# Construct a linear model
pred = tf.add(tf.mul(X, W), b)
# Mean squared error
cost = tf.reduce_sum(tf.pow(pred-Y, 2))/(2*n_samples)
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试这样的事情,其中成本是一个函数,cost(x,y,w,b)我只想要相对于渐变w and b:
grads = tf.gradients(cost, tf.all_variable())
Run Code Online (Sandbox Code Playgroud)
我的占位符也将包括在内(X和Y).即使我确实得到了一个渐变,[x,y,w,b]我怎么知道渐变中哪个元素属于每个参数,因为它只是一个没有名称的列表,哪个参数与衍生物有关?
我刚开始使用Tensorflow,我有一个新手问题.
我知道Tensorflow是关于神经网络的,但我只是从它的机制开始.我试图让它加载,调整大小,翻转和保存两个图像.应该是一个简单的操作,对,它让我从基础开始.
到目前为止,这是我的代码:
import tensorflow as tf
import numpy as np
print("resizing images")
filenames = ['img1.png', 'img2.png' ]
filename_queue = tf.train.string_input_producer(filenames, num_epochs=1)
reader = tf.WholeFileReader()
key,value = reader.read(filename_queue)
images = tf.image.decode_png(value)
resized = tf.image.resize_images(images, 180,180, 1)
resized.set_shape([180,180,3])
flipped_images = tf.image.flip_up_down(resized)
resized_encoded = tf.image.encode_jpeg(flipped_images,name="save_me")
init = tf.initialize_all_variables()
sess = tf.Session()
with sess.as_default():
tf.train.start_queue_runners()
sess.run(init)
f = open("/tmp/foo1.jpeg", "wb+")
f.write(resized_encoded.eval())
f.close()
f = open("/tmp/foo2.jpeg", "wb+")
f.write(resized_encoded.eval())
f.close()
Run Code Online (Sandbox Code Playgroud)
它工作正常,调整两个图像的大小并保存它们.但它始终以错误结束:
W tensorflow/core/common_runtime/executor.cc:1076] 0x7f97240e7a40
Compute status: Out of range: Reached limit of 1
Run Code Online (Sandbox Code Playgroud)
我显然做错了什么.如果我取消num_epochs = …
import tensorflow as tf
def activation(e, f, g):
return e + f + g
with tf.Graph().as_default():
a = tf.constant([5, 4, 5], name='a')
b = tf.constant([0, 1, 2], name='b')
c = tf.constant([5, 0, 5], name='c')
res = activation(a, b, c)
init = tf.initialize_all_variables()
with tf.Session() as sess:
# Start running operations on the Graph.
merged = tf.merge_all_summaries()
sess.run(init)
hi = sess.run(res)
print hi
writer = tf.train.SummaryWriter("/tmp/basic", sess.graph_def)
Run Code Online (Sandbox Code Playgroud)
输出错误:
Value Error: Fetch argument <tf.Tensor 'add_1:0' shape=(3,) dtype=int32> of
<tf.Tensor 'add_1:0' shape=(3,) dtype=int32> …Run Code Online (Sandbox Code Playgroud) 我想重用Tensorflow"MNIST for Pros"CNN示例中的代码.我的图像是388px X 191px,只有2个输出类.原始代码可以在这里找到.我试图通过仅更改输入和输出层来重用此代码,如下所示:
x = tf.placeholder("float", shape=[None, 74108])
y_ = tf.placeholder("float", shape=[None, 2])
x_image = tf.reshape(x, [-1,388,191,1])
Run Code Online (Sandbox Code Playgroud)
W_fc2 = weight_variable([1024, 2])
b_fc2 = bias_variable([2])
Run Code Online (Sandbox Code Playgroud)
运行修改后的代码会产生模糊的堆栈跟踪:
W tensorflow/core/common_runtime/executor.cc:1027] 0x2136510 Compute status: Invalid argument: Input has 14005248 values, which isn't divisible by 3136
[[Node: Reshape_4 = Reshape[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](MaxPool_5, Reshape_4/shape)]]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1267, in run
_run_using_default_session(self, feed_dict, self.graph, session)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", …Run Code Online (Sandbox Code Playgroud) python convolution neural-network conv-neural-network tensorflow
我正在尝试预测时间序列:给定50个先前的值,我想预测下5个值.
为此,我正在使用该skflow包(基于TensorFlow),这个问题与Github repo中提供的Boston示例相对接近.
我的代码如下:
%matplotlib inline
import pandas as pd
import skflow
from sklearn import cross_validation, metrics
from sklearn import preprocessing
filepath = 'CSV/FILE.csv'
ts = pd.Series.from_csv(filepath)
nprev = 50
deltasuiv = 5
def load_data(data, n_prev = nprev, delta_suiv=deltasuiv):
docX, docY = [], []
for i in range(len(data)-n_prev-delta_suiv):
docX.append(np.array(data[i:i+n_prev]))
docY.append(np.array(data[i+n_prev:i+n_prev+delta_suiv]))
alsX = np.array(docX)
alsY = np.array(docY)
return alsX, alsY
X, y = load_data(ts.values)
# Scale data to 0 mean and unit std dev.
scaler …Run Code Online (Sandbox Code Playgroud) 在Tensorflow读数数据教程中,给出了一个示例输入管道.在该管道中,数据被洗牌两次,无论是在内部string_input_producer还是在内部shuffle batch generator.这是代码:
def input_pipeline(filenames, batch_size, num_epochs=None):
# Fist shuffle in the input pipeline
filename_queue = tf.train.string_input_producer(
filenames, num_epochs=num_epochs, shuffle=True)
example, label = read_my_file_format(filename_queue)
min_after_dequeue = 10000
capacity = min_after_dequeue + 3 * batch_size
# Second shuffle as part of the batching.
# Requiring min_after_dequeue preloaded images
example_batch, label_batch = tf.train.shuffle_batch(
[example, label], batch_size=batch_size, capacity=capacity,
min_after_dequeue=min_after_dequeue)
return example_batch, label_batch
Run Code Online (Sandbox Code Playgroud)
第二次洗牌是否有用?混洗批量生成器的缺点在于,min_after_dequeue示例总是被预先存储在存储器中以允许有用的混洗.我的图像数据确实很大,内存消耗很大.这就是我考虑使用的原因normal batch generator.将数据混洗两次有什么好处吗?
编辑:附加问题,为什么string_input_producer初始化只有默认容量32?将batch_size的倍数作为容量不是有利的吗?
如何使用谓词函数过滤存储在队列中的数据?例如,假设我们有一个存储功能和标签张量的队列,我们只需要满足谓词的那些.我尝试了以下实现但没有成功:
feature, label = queue.dequeue()
if (predicate(feature, label)):
enqueue_op = another_queue.enqueue(feature, label)
Run Code Online (Sandbox Code Playgroud) 我想获得在1D张量中出现多次的元素.确切地说,我想创建一个与之相反的功能tf.unique.例如,如果x = [1, 1, 2, 3, 4, 5, 6, 7, 4, 5, 4]我需要输出,[1,1,4,4,4,5,5]同时还检索源张量中的那些元素的索引.我的最终目标是在批处理中获取标签出现多次的示例.