我一直试图从Tensorflow教程创建卷积网络,但我遇到了麻烦.出于某种原因,我遇到的错误是y_conv的大小比y_的大小大4倍,我不明白为什么.我发现了这个问题,但它似乎与我的问题不同,尽管它看起来很相似.
要清楚,下面代码中的批处理大小是50,但它出现的错误是
tensorflow.python.framework.errors.InvalidArgumentError:不兼容的形状:[200]与[50]
当我将批量大小更改为10时,我得到了
tensorflow.python.framework.errors.InvalidArgumentError:不兼容的形状:[40]与[10]
所以它与批量大小有某种关系,但我无法弄明白.谁能告诉我这段代码有什么问题?它与上面链接的教程非常相似.
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
import tensorflow as tf
sess = tf.InteractiveSession()
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides = [1, 2, 2, 1], padding='SAME')
x = tf.placeholder("float", shape=[None, 784])
y_ = tf.placeholder("float", shape=[None, 10])
w_conv1 …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Tensorflow构建高斯RBM模型.但该程序将使用太多内存.
gaussian_rbm.py
import tensorflow as tf
import math
import input_data
import numpy as np
def sample_prob(probs):
return tf.nn.relu(
tf.sign(
probs - tf.random_uniform(tf.shape(probs))))
class RBM(object):
""" represents a sigmoidal rbm """
def __init__(self, name, input_size, output_size, gaussian_std_val=0.1):
with tf.name_scope("rbm_" + name):
self.weights = tf.Variable(
tf.truncated_normal([input_size, output_size],
stddev=1.0 / math.sqrt(float(input_size))), name="weights")
self.v_bias = tf.Variable(tf.zeros([input_size]), name="v_bias")
self.h_bias = tf.Variable(tf.zeros([output_size]), name="h_bias")
self.input = tf.placeholder("float", shape=[None, 784])
#Gaussian
def_a = 1/(np.sqrt(2)*gaussian_std_val)
def_a = tf.constant(def_a, dtype=tf.float32)
self.a = tf.Variable( tf.ones(shape=[input_size]) * def_a,
name="a")
def propup(self, visible): …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Adagrad优化器构建CNN,但我收到以下错误.
tensorflow.python.framework.errors.FailedPreconditionError:试图使用未初始化值Variable_7/Adadelta
[[Node:Adadelta/update_Variable_7/ApplyAdadelta = ApplyAdadelta [T = DT_FLOAT,_class = ["loc:@ Variable_7"],use_locking = false,_device ="/ job:localhost/replica:0/task:0/cpu:0 "](Variable_7,Variable_7/Adadelta,Variable_7/Adadelta_1,Adadelta/lr,Adadelta/rho,Adadelta/epsilon,gradients/add_3_grad/tuple/control_dependency_1)]]由op u'Adadelta/update_Variable_7/ApplyAdadelta'引起,
optimizer = tf.train.AdadeltaOptimizer(learning_rate).minimize(cross_entropy)
我尝试在adagrad语句之后重新初始化会话变量,如本文所述,但这也没有帮助.
我怎样才能避免这个错误?谢谢.
import tensorflow as tf
import numpy
from tensorflow.examples.tutorials.mnist import input_data
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1], padding='SAME')
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)
# Parameters
learning_rate …Run Code Online (Sandbox Code Playgroud) 我在Tesla K80上使用CUDA 7.5和CUDNN v5运行Tensorflow 0.8.一切都很好,但两个设备无法互相访问.
警告日志如下所示.谢谢.
I tensorflow/core/common_runtime/gpu/gpu_init.cc:59] cannot enable peer access from device ordinal 0 to device ordinal 2
I tensorflow/core/common_runtime/gpu/gpu_init.cc:59] cannot enable peer access from device ordinal 0 to device ordinal 3
I tensorflow/core/common_runtime/gpu/gpu_init.cc:59] cannot enable peer access from device ordinal 1 to device ordinal 2
I tensorflow/core/common_runtime/gpu/gpu_init.cc:59] cannot enable peer access from device ordinal 1 to device ordinal 3
I tensorflow/core/common_runtime/gpu/gpu_init.cc:59] cannot enable peer access from device ordinal 2 to device ordinal 0
I tensorflow/core/common_runtime/gpu/gpu_init.cc:59] cannot enable …Run Code Online (Sandbox Code Playgroud) 我要去Tensorflow的教程.
我想显示的变量的值W和b,它们是重量和偏压分别和占位符x,y通过利用print.
可以显示吗?
print x,y,b,W
Run Code Online (Sandbox Code Playgroud)
我目前看到的如下
Tensor("Placeholder:0", shape=TensorShape([Dimension(None), Dimension(784)]), dtype=float32)
Tensor("Softmax:0", shape=TensorShape([Dimension(None), Dimension(10)]), dtype=float32)
tensorflow.python.ops.variables.Variable object at 0x1006b0b90>
tensorflow.python.ops.variables.Variable object at 0x101b76410>
Run Code Online (Sandbox Code Playgroud) 我试图通过向cifar10.py添加一些代码来编辑我自己的模型,这就是问题所在.
在cifar10.py中,[tutorial] [1]说:
练习:推理的输出是非标准化的logits.尝试使用tf.nn.softmax()编辑网络体系结构以返回规范化预测.
所以我直接输入"local4"的输出tf.nn.softmax().这给了我缩放的 logits,这意味着所有logits的总和是1.
但是在loss函数中,cifar10.py代码使用:
tf.nn.sparse_softmax_cross_entropy_with_logits()
Run Code Online (Sandbox Code Playgroud)
和这个功能的描述说
警告:此操作需要未缩放的日志,因为它在内部执行logmax以提高效率.不要使用softmax的输出调用此op,因为它会产生不正确的结果.
此外,根据描述,作为上述函数的输入的logits必须具有[batch_size,num_classes]的形状,并且它意味着logits应该是未缩放的softmax,如示例代码计算unnormalized softmaxlogit,如下所示.
# softmax, i.e. softmax(WX + b)
with tf.variable_scope('softmax_linear') as scope:
weights = _variable_with_weight_decay('weights', [192, NUM_CLASSES],
stddev=1/192.0, wd=0.0)
biases = _variable_on_cpu('biases', [NUM_CLASSES],
tf.constant_initializer(0.0))
softmax_linear = tf.add(tf.matmul(local4, weights), biases, name=scope.name)
_activation_summary(softmax_linear)
Run Code Online (Sandbox Code Playgroud)
这是否意味着我不必tf.nn.softmax在代码中使用?
今天我在Tensorflow中为我的LSTM添加学习率衰减.
我改变
train_op = tf.train.RMSPropOptimizer(lr_rate).minimize(loss)
Run Code Online (Sandbox Code Playgroud)
至
lr = tf.Variable(0.0,trainable=False)
Run Code Online (Sandbox Code Playgroud)
并运行每一个火车步骤
sess.run(tf.assign(lr, lr_rate*0.9**epoch))
Run Code Online (Sandbox Code Playgroud)
但是,这种变化会将执行时间从大约7分钟增加到大约20分钟.
我的问题是:为什么这种改变会增加执行时间?
一个明显的解决方法是每1000次迭代只进行一次分配.但是,我想了解这背后的原因.
我正在尝试使用CUDA支持安装Tensorflow.这是我的规格:
我已经通过pip安装安装了Tensorflow - 所以我想象你的答案是从源代码安装,但我想确保没有快速修复.
错误是:
volcart@volcart-Precision-Tower-7910:~$ python
Python 2.7.10 (default, Oct 14 2015, 16:09:02)
[GCC 5.2.1 20151010] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcublas.so locally
I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcudnn.so locally
I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcufft.so locally
I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcuda.so.1 locally
I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library …Run Code Online (Sandbox Code Playgroud) 我的代码是预测句子的情绪.我训练了一个CNN模型并保存了它.当我加载我的模型并尝试预测句子的情绪时,我对同一个句子有不同的预测.我的代码如下,当我尝试通过调用底部的函数predict_cnn_word2vec来预测sentene时,问题就出现了:
import logging;
import numpy as np;
import tensorflow as tf;
import sklearn as sk
import re;
import json
import string;
import math
import os
from sklearn.metrics import recall_score, f1_score, precision_score;
class CNN(object):
def __init__(self,logger):
self.logger = logger;
def _weight_variable(self,shape):
initial = tf.truncated_normal(shape, stddev = 0.1);
return tf.Variable(initial);
def _bias_variable(self,shape):
initial = tf.constant(0.1, shape = shape);
return tf.Variable(initial);
def _conv2d(self,x, W, b, strides=1):
# convolve and relu activation
x = tf.nn.conv2d(x, W, strides=[1, strides, strides, 1], padding='SAME');
x = tf.nn.bias_add(x, …Run Code Online (Sandbox Code Playgroud) machine-learning sentiment-analysis conv-neural-network tensorflow
对于在第一个转换层之后的转换层,Tensorflow梯度始终为零.我已经尝试了不同的方法来检查,但渐变总是为零!这是可以运行以检查的小型可重现代码.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
import numpy as np
import math
import os
import random
import tflearn
batch_size = 100
start = 0
end = batch_size
learning_rate = 0.000001
num_classes = 4
time_steps = 4
embedding = 2
step = 1
_units = 500
num_of_filters = 1000
train_set_x = [[[1,2],[3,4],[5,6],[7,8]],[[1,2],[3,4],[5,6],[7,8]]]
train_set_y = [0,1]
X = tf.placeholder(tf.float32, [None,time_steps,embedding])
Y = tf.placeholder(tf.int32, [None])
x = tf.expand_dims(X,3)
filter_shape = [1, embedding, …Run Code Online (Sandbox Code Playgroud) python mathematical-optimization gradient-descent tensorflow