# Define a Toy dataset.
x_train = np.array([[3.3],[4.4],[5.5],[6.71],[6.93],[4.168],[9.779],[6.182],[7.59],[2.167],[7.042],[10.791],[5.313],[7.997],[3.1]], dtype=np.float32)y_train = np.array([[1.7],[2.76],[2.09],[3.19],[1.694],[1.573],[3.366],[2.596],[2.53],[1.221],[2.827],[3.465],[1.65],[2.904],[1.3]], dtype=np.float32)# Confirm the data shape.print(x_train.shape, y_train.shape)
(15, 1) (15, 1)
# Linear regression model
model = nn.Linear(input_size, output_size)
# Loss and optimizer
criterion = nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate,)
# Train the modelfor epoch inrange(num_epochs):# Convert numpy arrays to torch tensorsinputs = torch.from_numpy(x_train)targets = torch.from_numpy(y_train)# Forward passoutputs = model(inputs)loss = criterion(outputs, targets)# Backward and optimizeoptimizer.zero_grad()loss.backward()optimizer.step()# Set an output counterif(epoch+1)%5==0:print('Epoch [{}/{}], loss: {:.4f}'.format(epoch+1, num_epochs, loss.item()))# Plot the graph
predicted = model(torch.from_numpy(x_train)).detach().numpy()
plt.plot(x_train, y_train,'ro', label='Original data')
plt.plot(x_train, predicted, label='Fitted line')
plt.legend()
plt.show()
目录 1 主要内容
2 部分代码
3 程序结果
4 下载链接 1 主要内容
该程序复现《Multi-Micro-Grid Main Body Electric Heating Double-Layer Sharing Strategy Based on Nash Game》模型,主要做的是构建基于纳什博弈的多微网主体电热双层共享模型,…
我们将创建一个具有CTC损失的卷积循环神经网络来实现我们的OCR识别模型。 一、数据集 我们将使用 Visual Geometry Group 提供的数据。
Visual Geometry Group - University of OxfordComputer Vision group from the University of Oxfordhttps://www.robots.ox.ac.uk/~vgg/d…