获取数据模板
import torch import torch.nn as nn import torchvision.datasets as dsets import torchvision.transforms as transforms from torch.autograd import Variable from torch.nn import Linear import scipy import numpy as np import matplotlib.pyplot as plt # x1 = np.loadtxt('2-negtive-mixed-15features.txt') # x2 = np.loadtxt('2-positive-mixed-15features.txt') def get_data(): train_X = np.asarray([2.0, 3.0, 4.0, 6.0]) train_Y = np.asarray([5.0, 3.0, 9.0, 7.0]) print(train_X) dtype = torch.FloatTensor X = Variable(torch.from_numpy(train_X).type(dtype), requires_grad=True).view(4,1) Y = Variable(torch.from_numpy(train_Y).type(dtype), requires_grad=False) return X,Y def get_weight(): w = Variable(torch.randn(1),requires_grad = True) b = Variable(torch.randn(1),requires_grad = True) return w, b def simple_network(): y_pred = torch.matmul(x,w)+b return y_pred inp = Variable(torch.ones(1,10)) myLayer = Linear(in_features=10,out_features=5,bias=True) myLayer(inp) x,y = get_data() w,b = get_weight() yhat = simple_network() print(x) print(y) print(w) print(b) print(yhat) print(myLayer(inp)) print(myLayer.weight) print(myLayer.bias)