我的问题有两个相互关联的部分:
如何计算张量的某个轴上的最大值?例如,如果我有
x = tf.constant([[1,220,55],[4,3,-1]])
Run Code Online (Sandbox Code Playgroud)
我想要类似的东西
x_max = tf.max(x, axis=1)
print sess.run(x_max)
output: [220,4]
Run Code Online (Sandbox Code Playgroud)
我知道有一个tf.argmax和一个tf.maximum,但是没有沿着单个张量的轴给出最大值.现在我有一个解决方法:
x_max = tf.slice(x, begin=[0,0], size=[-1,1])
for a in range(1,2):
x_max = tf.maximum(x_max , tf.slice(x, begin=[0,a], size=[-1,1]))
Run Code Online (Sandbox Code Playgroud)
但它看起来不太理想.有一个更好的方法吗?
给定argmax张量的指数,我如何使用这些指数索引另一个张量?使用x上面的示例,我如何执行以下操作:
ind_max = tf.argmax(x, dimension=1) #output is [1,0]
y = tf.constant([[1,2,3], [6,5,4])
y_ = y[:, ind_max] #y_ should be [2,6]
Run Code Online (Sandbox Code Playgroud)
我知道切片,就像最后一行一样,在TensorFlow中还不存在(#206).
我的问题是:对于我的特定情况,最好的解决方法是什么(可能使用其他方法,如收集,选择等)?
附加信息:我知道x并且y只会是二维张量!
例如,我有1维向量的维度(5).我想将其重塑为2D矩阵(1,5).
这是我如何用numpy做的
>>> import numpy as np
>>> a = np.array([1,2,3,4,5])
>>> a.shape
(5,)
>>> a = np.reshape(a, (1,5))
>>> a.shape
(1, 5)
>>> a
array([[1, 2, 3, 4, 5]])
>>>
Run Code Online (Sandbox Code Playgroud)
但是我怎么能用Pytorch Tensor(和Variable)做到这一点.我不想切换回numpy并再次切换到Torch变量,因为它会丢失反向传播信息.
这就是我在Pytorch中所拥有的
>>> import torch
>>> from torch.autograd import Variable
>>> a = torch.Tensor([1,2,3,4,5])
>>> a
1
2
3
4
5
[torch.FloatTensor of size 5]
>>> a.size()
(5L,)
>>> a_var = variable(a)
>>> a_var = Variable(a)
>>> a_var.size()
(5L,)
.....do some calculation in forward function
>>> a_var.size()
(5L,) …Run Code Online (Sandbox Code Playgroud) 我正在实现自己的keras损失功能.如何访问张量值?
我试过的
def loss_fn(y_true, y_pred):
print y_true
Run Code Online (Sandbox Code Playgroud)
它打印
Tensor("target:0", shape=(?, ?), dtype=float32)
Run Code Online (Sandbox Code Playgroud)
是否有任何keras函数来访问y_true值?
ipdb> outputs.size()
torch.Size([10, 100])
ipdb> print sum(outputs,0).size(),sum(outputs,1).size(),sum(outputs,2).size()
(100L,) (100L,) (100L,)
Run Code Online (Sandbox Code Playgroud)
如何对列进行求和?
在我学会了如何使用之后einsum,我现在正试图了解它是如何np.tensordot工作的.
但是,我有点迷失,特别是关于参数的各种可能性axes.
要理解它,因为我从未练习过张量微积分,我使用以下示例:
A = np.random.randint(2, size=(2, 3, 5))
B = np.random.randint(2, size=(3, 2, 4))
Run Code Online (Sandbox Code Playgroud)
在这种情况下,有什么不同可能np.tensordot,你会如何手动计算?
我正在学习tensorflow,我从tensorflow网站上获取了以下代码.根据我的理解,axis = 0表示行,axis = 1表示列.
他们如何获得评论中提到的输出?我根据我对##的想法提到了输出.
import tensorflow as tf
x = tf.constant([[1, 1, 1], [1, 1, 1]])
tf.reduce_sum(x, 0) # [2, 2, 2] ## [3, 3]
tf.reduce_sum(x, 1) # [3, 3] ##[2, 2, 2]
tf.reduce_sum(x, [0, 1]) # 6 ## Didn't understood at all.
Run Code Online (Sandbox Code Playgroud) 在Pytorch中似乎有几种创建张量副本的方法,包括
y = tensor.new_tensor(x) #a
y = x.clone().detach() #b
y = torch.empty_like(x).copy_(x) #c
y = torch.tensor(x) #d
Run Code Online (Sandbox Code Playgroud)
b明确优于a并d根据UserWarning如果我既可以执行我得到a或d。为什么首选它?性能?我认为它的可读性较差。
有/反对使用任何理由c?
我正在使用最后一层中的一些tensorflow函数(reduce_sum和l2_normalize)在Keras中构建模型,同时遇到此问题.我已经搜索了一个解决方案,但所有这些都与"Keras tensor"有关.
这是我的代码:
import tensorflow as tf;
from tensorflow.python.keras import backend as K
vgg16_model = VGG16(weights = 'imagenet', include_top = False, input_shape = input_shape);
fire8 = extract_layer_from_model(vgg16_model, layer_name = 'block4_pool');
pool8 = MaxPooling2D((3,3), strides = (2,2), name = 'pool8')(fire8.output);
fc1 = Conv2D(64, (6,6), strides= (1, 1), padding = 'same', name = 'fc1')(pool8);
fc1 = Dropout(rate = 0.5)(fc1);
fc2 = Conv2D(3, (1, 1), strides = (1, 1), padding = 'same', name = 'fc2')(fc1);
fc2 = Activation('relu')(fc2);
fc2 = Conv2D(3, (15, 15), …Run Code Online (Sandbox Code Playgroud) 我在这里关注PyTorch教程.它说
x = torch.randn(3, requires_grad=True)
y = x * 2
while y.data.norm() < 1000:
y = y * 2
print(y)
Out:
tensor([-590.4467, 97.6760, 921.0221])
Run Code Online (Sandbox Code Playgroud)
有人可以解释一下data.norm()在这里做什么吗?当我改变.randn到.ones它的输出 tensor([ 1024., 1024., 1024.]).