- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想根据索引在张量中赋值。
例如,根据tf.nn.max_pool_with_argmax的pooling值和对应的指标输出,我想将这些池值与索引一起放回原始的非池化张量中。
我发现 tf.nn.max_pool_with_argmax
的输出索引被展平了。一个问题:如何将它们分解回 Tensorflow 中的坐标?
另一个问题:在给定索引的情况下,如何将池化张量的每个值分配给原始非池化张量在 Tensorflow 中的位置?
非常感谢。
我试图编写代码来实现这一点,但我只能使用 numpy
。我不知道如何在 tf.nn.max_pool_with_argmax
之后获取扁平化索引并分配到 Tensorflow 中的 unpooling 张量。
ksize = 3
stride = 1
input_image = tf.placeholder(tf.float32, name='input_image')
#conv1
kernel = tf.Variable(tf.truncated_normal([ksize, ksize, 3, 16],stddev=0.1),
name='kernel')
conv = tf.nn.conv2d(input_image, kernel, [1,stride,stride,1], padding='SAME')
biases = tf.Variable(tf.constant(0.0, shape = [16]), name = 'biases')
bias = tf.nn.bias_add(conv, biases)
conv1 = tf.nn.relu(bias, name='conv1')
#pool1
pool1, pool1_indices = tf.nn.max_pool_with_argmax(conv1, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1],
padding='SAME', name='pool1')
#upsample by assigning the values of pool1 to the position in unpooling Tensor according to pool1_indices
indices = pool1_indices
unravel_pool1_indices = np.unravel_index(indices,[4,32,32,16])
unravel_pool1_coordinates = np.array(unravel_pool1_indices)
coor_shape = np.shape(unravel_pool1_coordinates)
unravel_pool1_coordinates = np.reshape(unravel_pool1_coordinates,(coor_shape[0],coor_shape[1]*coor_shape[2]*coor_shape[3]*coor_shape[4]))
unravel_pool1_coordinates = unravel_pool1_coordinates.T
values = pool1
values = np.reshape(values,(np.size(values)))
up1 = tf.constant(0.0, shape = [4,32,32,16])
delta = tf.SparseTensor(unravel_pool1_coordinates, values, shape = [4,32,32,16])
result = up1 + tf.sparse_tensor_to_dense(delta)
with tf.Session() as session:
session.run(tf.initialize_all_variables())
test_image = np.random.rand(4,32,32,3)
sess_outputs = session.run([pool1, pool1_indices],
{input_image.name: test_image})
最佳答案
有一个悬而未决的 PR 应该可以解决这个问题:
关于python - Tensorflow中如何根据索引给Tensor赋值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37679697/
我试图将迁移学习应用于 InceptionV3。这是我的代码: inception_model = InceptionV3(weights='imagenet',include_top=False)
我正在尝试展示 GAN 网络在某些指定时期的结果。打印当前结果的功能以前与 TF 一起使用。我需要换成 pytorch。 def show_result(G_net, z_, num_epoch, s
我对孪生神经网络还很陌生,最近发现了 this example和 Colab notebook . 运行代码时出现以下错误: IndexError: invalid index of a 0-dim
我正在尝试使用在此 PR 中添加的“高级”、numpy 样式的切片,但是我遇到了 same issue as the user here : ValueError: Shape must be ran
我想在 TensorFlow 中做类似这段 Numpy 代码的事情: a = np.zeros([5, 2]) idx = np.random.randint(0, 2, (5,)) row_idx
我有以下特征张量: Eigen::Tensor m(3,10,10); 我想访问第一个矩阵。在 numpy 中我会这样做 m(0,:,:) 我如何在 Eigen 中做到这一点 最佳答案 您可以使用 .
1、问题 模型训练完后进行测试,报错 RuntimeError: Tensor for 'out' is on CPU, Tensor for argument #1 'self' is on CPU
我正在对 TFRecords 进行配对,它为我提供了一个标签作为数值。但是我需要在读取原始记录时将此值转换为分类向量。我怎样才能做到这一点。这是读取原型(prototype)记录的代码片段: def
我正在对 TFRecords 进行配对,它为我提供了一个标签作为数值。但是我需要在读取原始记录时将此值转换为分类向量。我怎样才能做到这一点。这是读取原型(prototype)记录的代码片段: def
我应该如何从 Eigen::Tensor 创建一个 tensorflow::Tensor?我可以一个接一个地复制元素,但我希望有更好的方法。 最佳答案 没有公共(public) api 可以在不复制数
我正在尝试使用 Tensorflow(版本 0.9.0)以与 beginner's tutorial 非常相似的方式训练一个简单的二元逻辑回归分类器。并且在拟合模型时遇到以下错误: ValueErro
从 0.4.0 版本开始,可以使用 torch.tensor 和 torch.Tensor 有什么区别?提供这两个非常相似且令人困惑的替代方案的原因是什么? 最佳答案 在 PyTorch 中,torc
PyTorch0.4中,.data 仍保留,但建议使用 .detach(), 区别在于 .data 返回和 x 的相同数据 tensor, 但不会加入到x的计算历史里,且require s_grad
我有一个参差不齐的张量,在尝试创建模型并使用 model.fit() 时,出现错误:TypeError: Failed to convert object of type to Tensor. Co
我必须用生成器和判别器训练一个 GAN 网络。我的发电机网络如下。 def Generator(image_shape=(512,512,3): inputs = Input(image_shap
我正在使用 Flask 运行 Web 服务器,当我尝试使用 vgg16 时出现错误,vgg16 是 keras 的预训练 VGG16 模型的全局变量。我不知道为什么会出现这个错误,也不知道它是否与 T
我正在使用 keras 的预训练模型,并且在调用 ResNet50(weights='imagenet') 时出现错误。 我在 flask 服务器中有以下代码: def getVGG16Predict
执行以下代码时出现以下错误。 rnn.rnn() 返回张量列表。错误在 convert_to_tensor 行。 TypeError: List of Tensors when single Tens
我有一个fruit_train_net.py 文件,其中包含以下代码 import tensorflow as tf import numpy as np import time import os
我们可以使用 torch.Tensor([1., 2.], device='cuda') 在 GPU 上分配张量.使用这种方式而不是torch.cuda.Tensor([1., 2.])有什么不同吗?
我是一名优秀的程序员,十分优秀!