草庐IT

python - 如何打印 Keras 张量的值?

我正在实现自己的Keras损失函数。如何访问张量值?我尝试过的defloss_fn(y_true,y_pred):printy_true打印出来Tensor("target:0",shape=(?,?),dtype=float32)是否有任何Keras函数可以访问y_true值? 最佳答案 Keras的后端有print_tensor使您能够做到这一点。你可以这样使用它:importkeras.backendasKdefloss_fn(y_true,y_pred):y_true=K.print_tensor(y_true,messag

python - 在 TensorFlow 中,如何使用 python 从张量中获取非零值及其索引?

我想做这样的事情。假设我们有一个张量A。A=[[1,0],[0,4]]我想从中获取非零值及其索引。Nonzerovalues:[1,4]Nonzeroindices:[[0,0],[1,1]]Numpy中也有类似的操作。np.flatnonzero(A)返回展平A中非零的索引。x.ravel()[np.flatnonzero(x)]根据非零索引提取元素。这里是alink用于这些操作。如何使用python在Tensorflow中执行上述Numpy操作?(矩阵是否展平并不重要。) 最佳答案 您可以使用not_equal在Tensorfl

python - 如何在 Theano 中分配/更新张量共享变量的子集?

在theano中编译函数时,可以通过指定updates=[(X,new_value)]来更新共享变量(比如X)。现在我正在尝试仅更新共享变量的子集:fromtheanoimporttensorasTfromtheanoimportfunctionimportnumpyX=T.shared(numpy.array([0,1,2,3,4]))Y=T.vector()f=function([Y],updates=[(X[2:4],Y)]#erroroccur:#'updatetargetmust#beaSharedVariable'代码会引发错误“更新目标必须是共享变量”,我猜这意味着更新目

python - TensorFlow - 类似 numpy 的张量索引

在numpy中,我们可以这样做:x=np.random.random((10,10))a=np.random.randint(0,10,5)b=np.random.randint(0,10,5)x[a,b]#gives5entriesfromx,indexedaccordingtothecorrespondingentriesinaandb当我在TensorFlow中尝试类似的东西时:xt=tf.constant(x)at=tf.constant(a)bt=tf.constant(b)xt[at,bt]最后一行给出了“Badsliceindextensor”异常。TensorFlow似

python - 批量计算成对距离而不在Tensorflow中复制张量?

我想在Tensorflow中计算一批特征的成对平方距离。我有一个使用+和*操作的简单实现平铺原始张量:defpairwise_l2_norm2(x,y,scope=None):withtf.op_scope([x,y],scope,'pairwise_l2_norm2'):size_x=tf.shape(x)[0]size_y=tf.shape(y)[0]xx=tf.expand_dims(x,-1)xx=tf.tile(xx,tf.pack([1,1,size_y]))yy=tf.expand_dims(y,-1)yy=tf.tile(yy,tf.pack([1,1,size_x])

python - TensorFlow:沿轴的张量的最大值

我的问题是两个相关的部分:如何计算张量某个轴的最大值?例如,如果我有x=tf.constant([[1,220,55],[4,3,-1]])我想要类似的东西x_max=tf.max(x,axis=1)printsess.run(x_max)output:[220,4]我知道有一个tf.argmax和一个tf.maximum,但都没有给出沿单个张量轴的最大值。现在我有一个解决方法:x_max=tf.slice(x,begin=[0,0],size=[-1,1])forainrange(1,2):x_max=tf.maximum(x_max,tf.slice(x,begin=[0,a],s

python - Tensorflow Slim : TypeError: Expected int32, 得到了包含 '_Message' 类型张量的列表

我关注this学习TensorFlowSlim的教程,但在运行以下Inception代码时:importnumpyasnpimportosimporttensorflowastfimporturllib2fromdatasetsimportimagenetfromnetsimportinceptionfrompreprocessingimportinception_preprocessingslim=tf.contrib.slimbatch_size=3image_size=inception.inception_v1.default_image_sizecheckpoints_dir

python - 如何在图构建时获取张量的维度(在 TensorFlow 中)?

我正在尝试一个不符合预期的操作。graph=tf.Graph()withgraph.as_default():train_dataset=tf.placeholder(tf.int32,shape=[128,2])embeddings=tf.Variable(tf.random_uniform([50000,64],-1.0,1.0))embed=tf.nn.embedding_lookup(embeddings,train_dataset)embed=tf.reduce_sum(embed,reduction_indices=0)所以我需要知道张量embed的尺寸。我知道它可以在运行

python - 在张量板上记录训练和验证损失

我正在尝试学习如何使用tensorflow和tensorboard。我有一个基于MNISTneuralnettutorial的测试项目.在我的代码中,我构建了一个节点,用于计算数据集中正确分类的数字比例,如下所示:correct=tf.nn.in_top_k(self._logits,labels,1)correct=tf.to_float(correct)accuracy=tf.reduce_mean(correct)这里,self._logits是图的推理部分,labels是包含正确标签的占位符。现在,我想做的是在训练进行时评估训练集和验证集的准确性。我可以通过使用不同的feed_

python - Tensorflow:如何按名称获取张量?

我无法按名称恢复张量,我什至不知道这是否可能。我有一个函数可以创建我的图表:defcreate_structure(tf,x,input_size,dropout):withtf.variable_scope("scale_1")asscope:W_S1_conv1=deep_dive.weight_variable_scaling([7,7,3,64],name='W_S1_conv1')b_S1_conv1=deep_dive.bias_variable([64])S1_conv1=tf.nn.relu(deep_dive.conv2d(x_image,W_S1_conv1,str