我正在使用Pytorch处理线性回归问题。
我在单变量情况下取得了成功,但是当我执行多变量线性回归时,出现以下错误。如何使用多个变量进行线性回归?
()中的TypeError Traceback(最近一次调用最后一次)(9)Optimizer.zero_grad()#梯度10输出=模型(输入)#输出---> 11损失=标准(输出,目标)#损失函数12 loss.backward() #向后传播13 Optimizer.step()#1步优化(gradeint下降)
/anaconda/envs/tensorflow/lib/python3.6/site-packages/torch/nn/modules/module.py在调用中(self,* input,** kwargs)204205 def 调用(self,* input,** kwargs):-> 206 result = self.forward(* input,** kwargs)207 for self中的hook._forward_hooks.values():208 hook_result = hook(self,input,result)
/anaconda/envs/tensorflow/lib/python3.6/site-packages/torch/nn/modules/loss.py in forward(自身,输入,目标)22 _assert_no_grad(目标)23 backend_fn = getattr(self._backend,类型(self .. name)---> 24 return backend_fn(self.size_average)(input,target)25 26
/anaconda/envs/tensorflow/lib/python3.6/site-packages/torch/nn/_functions/thnn/auto.py in forward(自己,输入,目标)39 output = input.new(1)40 getattr(self ._backend,update_output.name)(self._backend.library_state,input,target,---> 41 output,* self.additional_args)42返回输出43
TypeError:FloatMSECriterion_updateOutput接收到无效的参数组合-得到(int,torch.FloatTensor,torch.DoubleTensor,torch.FloatTensor,bool),但是是预期的(int状态,torch.FloatTensor输入,torch.FloatTensor目标,torch.Float布尔值(平均)
这是代码
#import
import torch
import torch.nn as nn
import numpy as np
import matplotlib.pyplot as plt
from torch.autograd import Variable
#input_size = 1
input_size = …Run Code Online (Sandbox Code Playgroud) 我想编写一个使用牛顿方法的程序:
估计这个积分的x:
其中X是总距离.
我有函数计算通过使用梯形方法进行数值积分到达一定距离所需的时间.不使用trapz.
function T = time_to_destination(x, route, n)
h=(x-0)/n;
dx = 0:h:x;
y = (1./(velocity(dx,route)));
Xk = dx(2:end)-dx(1:end-1);
Yk = y(2:end)+y(1:end-1);
T = 0.5*sum(Xk.*Yk);
end
Run Code Online (Sandbox Code Playgroud)
它通过一组数据点之间的三次样条插值的ppval获取其速度值.外推值不应该是可取的.
function [v] = velocity(x, route)
load(route);
if all(x >= distance_km(1))==1 & all(x <= distance_km(end))==1
estimation = spline(distance_km, speed_kmph);
v = ppval(estimation, x);
else
error('Bad input, please choose a new value')
end
end
Run Code Online (Sandbox Code Playgroud)
速度样条曲线的绘图如果您对此感兴趣,请评估:
dx= 1:0.1:65
Run Code Online (Sandbox Code Playgroud)

现在我想编写一个函数,可以解决在给定时间之后行进的距离,使用没有fzero/fsolve的newton方法.但我不知道如何解决积分的上界.
根据微积分的基本定理,我认为积分的导数是积分内的函数,这是我试图重新创建的Time_to_destination /(1/velocity)我添加了我想要解决的常数到时间目的地所以它
(Time_to_destination - (输入时间))/(1 /速度)
不确定我是否正确行事.
编辑:重写我的代码,现在效果更好,但我对Newton Raphson的停止条件似乎没有收敛到零.我也试图从梯形积分(ET)实现错误,但不确定我是否应该打扰实现它.还可以在底部找到路径文件.
牛顿方法的停止条件和误差计算:
梯形误差估计:
Function x = distance(T, route) …Run Code Online (Sandbox Code Playgroud) 我有一个问题,我试图解决,创建一个Nx1单元格,其中存储的数据总是N个2x2矩阵.
例:
N = 2
mycell = cell(N,1);
for i =1:N;
mycell{i} = randi([0, 10], 2);
end
newmatrix = zeros (N+1);
Run Code Online (Sandbox Code Playgroud)
所以说mycell {1}看起来像:
[3 5
2 1]
Run Code Online (Sandbox Code Playgroud)
和mycell {2}看起来像:
[6 9;
3 2]
Run Code Online (Sandbox Code Playgroud)
我新的零矩阵看起来像:
[0 0 0
0 0 0
0 0 0]
Run Code Online (Sandbox Code Playgroud)
我想让它看起来像这样(在这种对角线设置中加入第一个单元格的最后一个元素与下一个单元格的第一个元素):
[3 5 0
2 7 9
0 3 2]
Run Code Online (Sandbox Code Playgroud)
有没有一种简单的方法可以做到这一点或任何可能有帮助的内置Matlab函数?
谢谢.
我想在我的模型在 Tensorflow 2.0 中训练时监控梯度流。理想情况下,我希望在模型训练时将梯度存储在数组中,然后在训练完成后通过 matplotlib 查看它们。我该怎么做?
这是我想要运行的玩具模型:
tf.keras.backend.clear_session()
input_sequence = tf.keras.layers.Input(shape=[number_timesteps, number_features])
x = tf.keras.layers.Conv1D(2**5, 3, padding='same', activation='relu')(input_sequence)
x = tf.keras.layers.Flatten()(x)
x = tf.keras.layers.Dropout(0.5)(x)
output_sequence = tf.keras.layers.Dense(number_classes, activation="softmax")(x)
model = tf.keras.Model(input_sequence, output_sequence)
model.compile(loss=tf.keras.losses.categorical_crossentropy, metrics=["accuracy"], optimizer=tf.keras.optimizers.Adam())
model.summary()
model.fit(...)
Run Code Online (Sandbox Code Playgroud) 所以我正在浏览非官方OpenGL库的glimg部分,并发现了一些我觉得很奇怪的东西.在其中一个函数中,指针参数被分配给自己,我无法看到它是如何完成任何事情的.这是以某种方式强制内存进入缓存还是其他东西?可能是一个错误?
static uint8 *resample_row_generic(uint8 *out, uint8 *in_near, uint8 *in_far, int w, int hs)
{
// resample with nearest-neighbor
int i,j;
in_far = in_far; // <-- here?
for (i=0; i < w; ++i)
for (j=0; j < hs; ++j)
out[i*hs+j] = in_near[i];
return out;
}
Run Code Online (Sandbox Code Playgroud) 我正在使用OpenGL 4.2,我无法弄清楚为什么我在这个程序中收到GL_INVALID_VALUE错误.当我调用glBindAttribLocation时出错.根据OpenGL 4参考页面,只有两个原因可以从glBindAttribLocation生成GL_INVALID_VALUE.
void glBindAttribLocation(GLuint program, GLuint index, const GLchar *name);
正如你可以从下面的程序看,条件1不设置,因为indexIS 20和GL_MAX_VERTEX_ATTRIBS是34921.不满足条件2,因为program是由OpenGL使用生成的glCreateProgram().那我怎么可能得到一个GL_INVALID_VALUE错误?
// test.cpp
#include <GL/glew.h>
#include <GL/glut.h>
#include <iostream>
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutCreateWindow("Test");
glewInit();
std::cout << "Max Vertex Attributes : " << GL_MAX_VERTEX_ATTRIBS << std::endl;
// create program
GLuint program = glCreateProgram();
if ( program == 0 )
std::cout << "Program error" << std::endl;
// clear …Run Code Online (Sandbox Code Playgroud) 手动完成一维卷积非常简单。然而,我想用它来实现这里nn.Conv1d所做的事情,这对我来说并不简单。在此示例中,h=[1,2,-1],x=[4,1,2,5],输出将为 y=[4,9,0,8,8,-5]。为了使用 Pytorch 做到这一点,我们需要定义h=nn.Conv1d(in, out, k)和 ,x=torch.tensor(*)并且y=h(x)应该是结果。
注意:请不要使用nn.Conv2d它来实现它。
我编写了这个 pytorch 程序来在 GPU 上计算 5000*5000 矩阵乘法,迭代 100 次。
import torch
import numpy as np
import time
N = 5000
x1 = np.random.rand(N, N)
######## a 5000*5000 matrix multiplication on GPU, 100 iterations #######
x2 = torch.tensor(x1, dtype=torch.float32).to("cuda:0")
start_time = time.time()
for n in range(100):
G2 = x2.t() @ x2
print(G2.size())
print("It takes", time.time() - start_time, "seconds to compute")
print("G2.device:", G2.device)
start_time2 = time.time()
# G4 = torch.zeros((5,5),device="cuda:0")
G4 = G2[:5, :5]
print("G4.device:", G4.device)
print("G4======", G4)
# G5=G4.cpu()
# print("G5.device:",G5.device) …Run Code Online (Sandbox Code Playgroud)