我是张力流的新手,并且一直在看这里的例子.我想将多层感知器分类模型重写为回归模型.但是在修改损失函数时遇到了一些奇怪的行为.它工作正常tf.reduce_mean,但如果我尝试使用tf.reduce_sum它在输出中给出nan.这看起来很奇怪,因为函数非常相似 - 唯一的区别是均值将总和结果除以元素数量?所以我看不出这种变化会如何引入nan?
import tensorflow as tf
# Parameters
learning_rate = 0.001
# Network Parameters
n_hidden_1 = 32 # 1st layer number of features
n_hidden_2 = 32 # 2nd layer number of features
n_input = 2 # number of inputs
n_output = 1 # number of outputs
# Make artificial data
SAMPLES = 1000
X = np.random.rand(SAMPLES, n_input)
T = np.c_[X[:,0]**2 + np.sin(X[:,1])]
# tf Graph input
x = tf.placeholder("float", [None, n_input])
y = …Run Code Online (Sandbox Code Playgroud) 当在PyTorch中使用双线性层时,我无法绕过计算的方式.
这是一个小例子,我试图弄清楚它是如何工作的:
在:
import torch.nn as nn
B = nn.Bilinear(2, 2, 1)
print(B.weight)
Run Code Online (Sandbox Code Playgroud)
日期:
Parameter containing:
tensor([[[-0.4394, -0.4920],
[ 0.6137, 0.4174]]], requires_grad=True)
Run Code Online (Sandbox Code Playgroud)
我正在通过一个零向量和一个向量.
在:
print(B(torch.ones(2), torch.zeros(2)))
print(B(torch.zeros(2), torch.ones(2)))
Run Code Online (Sandbox Code Playgroud)
日期:
tensor([0.2175], grad_fn=<ThAddBackward>)
tensor([0.2175], grad_fn=<ThAddBackward>)
Run Code Online (Sandbox Code Playgroud)
我尝试以各种方式加权,但我没有得到相同的结果.
提前致谢!
我按照说明编译并安装了 poppler-0.39.0。默认情况下,头文件进入 int\usr\local\include而 lib 文件进入\usr\local\lib. pdftohtml 安装在\usr\local\bin.
现在当我尝试运行pdftohtml它时出现以下错误。
pdftohtml: error while loading shared libraries: libpoppler.so.58: cannot open shared object file: No such file or directory.
Run Code Online (Sandbox Code Playgroud)
虽然libpoppler.so.58存在于\usr\local\lib. 请帮我。
我对tensorflow的新选项有一些麻烦,它允许我们运行分布式张量流.
我只想运行2个tf.constant和2个任务,但我的代码永远不会结束.它看起来像那样:
import tensorflow as tf
cluster = tf.train.ClusterSpec({"local": ["localhost:2222", "localhost:2223"]})
server = tf.train.Server(cluster,
job_name="local",
task_index=0)
with tf.Session(server.target) as sess:
with tf.device("/job:local/replica:0/task:0"):
const1 = tf.constant("Hello I am the first constant")
with tf.device("/job:local/replica:0/task:1"):
const2 = tf.constant("Hello I am the second constant")
print sess.run([const1, const2])
Run Code Online (Sandbox Code Playgroud)
我有以下代码(只有一个localhost:2222):
import tensorflow as tf
cluster = tf.train.ClusterSpec({"local": ["localhost:2222"]})
server = tf.train.Server(cluster,
job_name="local",
task_index=0)
with tf.Session(server.target) as sess:
with tf.device("/job:local/replica:0/task:0"):
const1 = tf.constant("Hello I am the first constant")
const2 = tf.constant("Hello I am the second constant")
print …Run Code Online (Sandbox Code Playgroud) 截至PyTorch 0.4,此问题已不再有效.在0.4 Tensor秒和Variables合并.
如何在PyTorch中使用变量和张量执行逐元素乘法?有两个张量工作正常.变量和标量工作正常.但是当尝试使用变量和张量执行逐元素乘法时,我得到:
XXXXXXXXXXX in mul
assert not torch.is_tensor(other)
AssertionError
Run Code Online (Sandbox Code Playgroud)
例如,运行以下内容时:
import torch
x_tensor = torch.Tensor([[1, 2], [3, 4]])
y_tensor = torch.Tensor([[5, 6], [7, 8]])
x_variable = torch.autograd.Variable(x_tensor)
print(x_tensor * y_tensor)
print(x_variable * 2)
print(x_variable * y_tensor)
Run Code Online (Sandbox Code Playgroud)
我希望第一个和最后一个打印语句显示类似的结果.前两个乘法按预期工作,错误在第三个中出现.我试图别名*在PyTorch(即x_variable.mul(y_tensor),torch.mul(y_tensor, x_variable)等).
考虑到错误和产生它的代码,似乎不支持张量和变量之间的元素乘法.它是否正确?还是有什么我想念的?谢谢!
我有问题,我无法用Keras和ThensorFlow重现我的结果.
似乎最近在Keras文档站点上发布了针对此问题的解决方法,但不知何故,它对我不起作用.
我做错了什么?
我正在MBP Retina上使用Jupyter笔记本(没有Nvidia GPU).
# ** Workaround from Keras Documentation **
import numpy as np
import tensorflow as tf
import random as rn
# The below is necessary in Python 3.2.3 onwards to
# have reproducible behavior for certain hash-based operations.
# See these references for further details:
# https://docs.python.org/3.4/using/cmdline.html#envvar-PYTHONHASHSEED
# https://github.com/fchollet/keras/issues/2280#issuecomment-306959926
import os
os.environ['PYTHONHASHSEED'] = '0'
# The below is necessary for starting Numpy generated random numbers
# in a well-defined initial state.
np.random.seed(42)
# …Run Code Online (Sandbox Code Playgroud) 我是PyTorch的新手.我遇到了一些包含各种不同示例的GitHub存储库(链接到完整代码示例).
还有一个关于LSTM的例子,这是Network类:
# RNN Model (Many-to-One)
class RNN(nn.Module):
def __init__(self, input_size, hidden_size, num_layers, num_classes):
super(RNN, self).__init__()
self.hidden_size = hidden_size
self.num_layers = num_layers
self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True)
self.fc = nn.Linear(hidden_size, num_classes)
def forward(self, x):
# Set initial states
h0 = Variable(torch.zeros(self.num_layers, x.size(0), self.hidden_size))
c0 = Variable(torch.zeros(self.num_layers, x.size(0), self.hidden_size))
# Forward propagate RNN
out, _ = self.lstm(x, (h0, c0))
# Decode hidden state of last time step
out = self.fc(out[:, -1, :])
return out
Run Code Online (Sandbox Code Playgroud)
所以我的问题是关于以下几行:
h0 = Variable(torch.zeros(self.num_layers, …Run Code Online (Sandbox Code Playgroud) 我有一个SimpleRNN喜欢:
model.add(SimpleRNN(10, input_shape=(3, 1)))
model.add(Dense(1, activation="linear"))
Run Code Online (Sandbox Code Playgroud)
模型摘要说:
simple_rnn_1 (SimpleRNN) (None, 10) 120
Run Code Online (Sandbox Code Playgroud)
我很好奇的参数个数120为simple_rnn_1。
有人能回答我的问题吗?
machine-learning neural-network deep-learning keras recurrent-neural-network
嗨,我有一个问题,关于如何从BI-LSTM模块的输出中收集正确的结果。
假设我有一个10长度的序列输入到具有100个隐藏单元的单层LSTM模块中:
lstm = nn.LSTM(5, 100, 1, bidirectional=True)
Run Code Online (Sandbox Code Playgroud)
output 形状如下:
[10 (seq_length), 1 (batch), 200 (num_directions * hidden_size)]
# or according to the doc, can be viewed as
[10 (seq_length), 1 (batch), 2 (num_directions), 100 (hidden_size)]
Run Code Online (Sandbox Code Playgroud)
如果我想同时获得两个方向上的第三个(1-索引)输入输出(两个100维矢量),我该如何正确执行呢?
我知道output[2, 0]会给我一个200维的向量。这个200个暗淡的向量代表两个方向上的第三个输入的输出吗?
令我困扰的是,当进行反向进给时,从第8个(1-索引)输入计算出第三个(1-索引)输出矢量,对吗?
pytorch会自动处理此问题并考虑方向分组输出吗?
谢谢!
冻结pytorch中的砝码以进行param_groups设置。
因此,如果您想在训练期间冻结体重:
for param in child.parameters():
param.requires_grad = False
Run Code Online (Sandbox Code Playgroud)
优化器也必须更新为不包括非梯度权重:
optimizer = torch.optim.Adam(filter(lambda p: p.requires_grad, model.parameters()), lr=opt.lr, amsgrad=True)
Run Code Online (Sandbox Code Playgroud)
如果要weight_decay对偏倚和权重使用不同的/学习率/这也允许不同的学习率:
param_groups = [{'params': model.module.bias_parameters(), 'weight_decay': args.bias_decay},
{'params': model.module.weight_parameters(), 'weight_decay': args.weight_decay}]
Run Code Online (Sandbox Code Playgroud)
param_groups定义了一个dic 列表,并将其传递SGD如下:
optimizer = torch.optim.Adam(param_groups, args.lr,
betas=(args.momentum, args.beta))
Run Code Online (Sandbox Code Playgroud)
冻结单个砝码如何实现?在dic列表上运行filter或是否可以将张量单独添加到优化器?
python ×5
pytorch ×5
tensorflow ×3
keras ×2
lstm ×2
distributed ×1
linux ×1
matrix ×1
path ×1
poppler ×1
python-3.x ×1
random ×1
server ×1
ubuntu ×1