25609 Problem I 习题5-10 分数序列求和

问题 I: 习题5-10 分数序列求和

时间限制: 1 Sec  内存限制: 12 MB

题目描述

有如下分数序列

2/1,3/2,5/3,8/5,13/8,21/13…

求出次数列的前20项之和。

请将结果的数据类型定义为double类型。

输入

输出

小数点后保留6位小数,末尾输出换行。

样例输入

样例输出

32.660261

经验总结

emmm,找规律题,分母就是斐波那契数列,分子就是这个分数的分母和上一个分数的分母之和~

AC代码

#include <cstdio>
int main()
{
	double b=1.0,c=1.0,sum=0;
	for(int i=1;i<=20;i++)
	{
		double temp=b+c;
		b=c;
		c=temp;
		sum+=c/b;
	}
	printf("%.6f\n",sum);
	return 0;
}

 

  • 7
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,我们需要导入必要的库。在这个例子中,我们需要使用PyTorch、NumPy和Matplotlib。 ```python import torch import torch.nn as nn import numpy as np import matplotlib.pyplot as plt ``` 接下来,我们需要准备我们的时间序列数据。我们将使用sin函数生成一个周期为50的时间序列,长度为200。 ```python # Generate time series data TIME_STEP = 50 SIN_WAVE_AMPLITUDE = 0.5 def sin(x, t): return np.sin(x + t) def toy_problem(T): x = np.arange(0, 2 * T + 1) return sin(2 * np.pi * x / T, 0), sin(2 * np.pi * x / T, 0.25), sin(2 * np.pi * x / T, 0.5) x_train, y_train, z_train = toy_problem(TIME_STEP) ``` 接下来,我们需要将数据转换为PyTorch张量,并将其分为训练集和测试集。 ```python # Convert data to PyTorch tensors x_train = torch.from_numpy(x_train).float().unsqueeze(1) y_train = torch.from_numpy(y_train).float().unsqueeze(1) z_train = torch.from_numpy(z_train).float().unsqueeze(1) # Split data into training and testing sets train_data = torch.cat((x_train, y_train, z_train), dim=1) train_input = train_data[:, :-1] train_target = train_data[:, 1:] train_input = train_input.view(-1, 2, TIME_STEP) train_target = train_target.view(-1, 2, TIME_STEP) ``` 现在我们已经准备好了我们的数据,我们可以开始构建我们的CNN和Bi-Transformer模型。我们将使用两个序列模块来处理每个时间步长的输入,然后将它们连接起来,并使用一个线性层来生成输出。 ```python # Define CNN and Bi-Transformer model class CNN(nn.Module): def __init__(self): super(CNN, self).__init__() self.conv = nn.Conv1d(2, 16, 5, padding=2) self.pool = nn.MaxPool1d(2) self.relu = nn.ReLU() def forward(self, x): x = self.conv(x) x = self.relu(x) x = self.pool(x) x = self.conv(x) x = self.relu(x) x = self.pool(x) return x class BiTransformer(nn.Module): def __init__(self): super(BiTransformer, self).__init__() self.pos_enc = PositionalEncoding(16, dropout=0.1) self.transformer = nn.Transformer(d_model=16, nhead=2, num_encoder_layers=2, num_decoder_layers=2, dim_feedforward=32, dropout=0.1) self.fc = nn.Linear(16, 1) def forward(self, x): x = self.pos_enc(x) x = self.transformer(x, x) x = self.fc(x) return x class PositionalEncoding(nn.Module): def __init__(self, d_model, dropout=0.1, max_len=1000): super(PositionalEncoding, self).__init__() self.dropout = nn.Dropout(p=dropout) pe = torch.zeros(max_len, d_model) position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(1) div_term = torch.exp(torch.arange(0, d_model, 2).float() * (-np.log(10000.0) / d_model)) pe[:, 0::2] = torch.sin(position * div_term) pe[:, 1::2] = torch.cos(position * div_term) pe = pe.unsqueeze(0).transpose(0, 1) self.register_buffer('pe', pe) def forward(self, x): x = x + self.pe[:x.size(0), :] return self.dropout(x) ``` 现在我们已经定义了我们的模型,我们可以开始训练它。我们将使用均方误差损失函数和随机梯度下降优化器。 ```python # Train the model model = nn.Sequential(CNN(), BiTransformer()) loss_fn = nn.MSELoss() optimizer = torch.optim.SGD(model.parameters(), lr=0.01) num_epochs = 1000 for epoch in range(num_epochs): optimizer.zero_grad() output = model(train_input) loss = loss_fn(output, train_target) loss.backward() optimizer.step() if epoch % 100 == 0: print('Epoch [{}/{}], Loss: {:.4f}'.format(epoch+1, num_epochs, loss.item())) # Generate predictions model.eval() with torch.no_grad(): test_input = train_data[:, :2, :] test_output = model(test_input) ``` 最后,我们可以将预测结果可视化,并将其与原始数据进行比较。 ```python # Plot predictions plt.plot(range(TIME_STEP), test_input[0, 0, :], label='input') plt.plot(range(TIME_STEP, 2*TIME_STEP), test_output[0, 0, :], label='prediction') plt.plot(range(TIME_STEP, 2*TIME_STEP), test_input[0, 1, :], label='target') plt.legend() plt.show() ``` 这就是如何使用PyTorch实现CNN和Bi-Transformer时间序列预测。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值