In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to use PyTorch to build CNN to achieve wind speed prediction, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.
Data set
The data set is the meteorological data of Barcelona for a certain period of time, including temperature, humidity, wind speed and so on. In this paper, CNN is used to predict the wind speed.
Characteristic structure
For the prediction of wind speed, in addition to considering the historical wind speed data, we should also fully consider the influence of other meteorological factors. Therefore, we predict the wind speed at the next moment based on the wind speed of the first 24 moments plus the rest of the meteorological data of the next moment.
One-dimensional convolution
We are familiar with the two-dimensional convolution of CNN when dealing with image data, which is a local operation, through a certain size of the convolution kernel acting on the local image region to obtain the local information of the image. The inner product (inner product) operation of data and convolution kernels from different data windows in an image is called convolution, and its essence is purification, that is, to extract the features of different frequency bands of the image.
The above paragraph is not easy to understand, let's give a simple example:
Suppose the leftmost channel of an input image is 5 × 55\ times55 × 5, and the middle is a layer of a convolution kernel, 3 × 33\ times33 × 3. We align the upper left of the convolution kernel with the upper left of the input, and then the whole convolution kernel can move to the right or down. Suppose that each time you move a small square, then the convolution kernel actually passes through an area of 3 × 33\ times33 × 3, so how to convolution? For example, at the beginning, the input is located in the upper left corner, and the input corresponds to (1,1,1), while the convolution layer is always (1,0,0), and the convolution layer is always (1,0,0). Let the two do the inner product operation, that is, 1 * 1 + (- 1 * 1) = 0, this 0 is the upper left corner of the result matrix. When the convolution kernel sweeps over the shaded part of the picture, the corresponding inner product is-1, as shown in the image above.
Therefore, two-dimensional convolution is the sliding window operation of a feature graph in both width and height directions, and the corresponding positions are multiplied and summed.
In contrast, one-dimensional convolution is usually used for time series prediction, while one-dimensional convolution is only to slide the window and multiply the sum in the direction of width or height. As shown in the following figure:
The original sequence number is: (1, 20, 15, 3, 18, 12. 4, 17), the dimension is 8. The dimension of convolution kernel is 5, and convolution kernel is (1, 3, 10, 3, 1). Then after the convolution kernel is applied to the above original data, the dimension of the data will become: 8-5-1-4. That is, the five numbers in the convolution kernel first convolution with the first five data in the original data, then move, convolution with the second to sixth data, and so on.
Data processing 1. Data preprocessing
In the data preprocessing stage, the text data on some columns are converted into numerical data, and the original data is normalized at the same time. The text data is as follows:
After conversion, each of the above categories is assigned different values, such as "sky is clear" is 0 and "few clouds" is 1.
Def load_data (): global Max, Min df = pd.read_csv ('Barcelona/Barcelona.csv') df.drop_duplicates (subset= [df.columns [0]], inplace=True) # weather_main listType = df [' weather_main'] .unique () df.fillna (method='ffill' Inplace=True) dic = dict.fromkeys (listType) for i in range (len (listType)): Dickey [df ['weather_main'] = df [' weather_main'] .map (dic) # weather_description listType = df ['weather_description'] .unique () dic = dict.fromkeys (listType) for i in range (len (listType)): Dickey [listType.I] = I df ['weather_description'] = df [' weather_description'] .map (dic) # weather_icon listType = df ['weather_icon'] .unique () dic = dict.fromkeys (listType) for i in range (len (listType)): dic [listType [I] = I df [' weather_icon'] = df ['weather_icon'] .map (dic) # print (df) columns = df.columns Max = np. Max (df ['wind_speed']) # Normalized Min = np.min (df [' wind_speed']) for i in range (2 17): column = columns [I] if column = 'wind_speed': continue df [column] = DF [column] .astype (' float64') if len (DF [df [column] = = 0]) = = len (df): # all 0 continue mx = np.max (DF [column]) mn = np.min (DF [co lumn]) df [column] = (df [column]-mn) / (mx-mn) # print (df.isna (). Sum ()) return df2. Data set construction
The meteorological data of the current moment and the wind speed data of the first 24 hours are used to predict the wind speed of the current moment:
Def nn_seq (): "": param flag:: param data: data to be processed: return: X and Y data sets, X = [year of the current time Month, hour, day, lowtemp, hightemp Load at the current moment of the previous day and 23 hours before] Y = [current load] "" print ('processing data:') data = load_data () speed = data ['wind_speed'] speed = speed.tolist () speed = torch.FloatTensor (speed). View (- 1) data = data.values.tolist () Seq = [] for i in range (len (data)-30): train_seq = [] train_label = [] for j in range (I I + 24): train_seq.append (speed [j]) # add temperature, humidity, air pressure and other information for c in range (2,7): train_seq.append (data [I + 24] [c]) for c in range (8 17): train_seq.append (data [I + 24] [c]) train_label.append (speed [I + 24]) train_seq = torch.FloatTensor (train_seq). View (- 1) train_label = torch.FloatTensor (train_label). View (- 1) seq.append ((train_seq) Train_label) # print (seq [: 5]) Dtr = seq [0:int (len (seq) * 0.5)] Den = seq [int (len (seq) * 0.50): int (len (seq) * 0.75)] Dte = seq [int (len (seq) * 0.75): len (seq)] return Dtr, Den, Dte
Arbitrarily output one of the pieces of data:
(tensor ([1.0000e+00, 1.0000e+00, 2.0000e+00, 1.0000e+00, 1.0000e+00, 1.0000e+00
1.0000e+00, 1.0000e+00, 0.0000e+00, 1.0000e+00, 5.0000e+00, 0.0000e+00
2.0000e+00, 0.0000e+00, 0.0000e+00, 5.0000e+00, 0.0000e+00, 2.0000e+00
2.0000e+00, 5.0000e+00, 6.0000e+00, 5.0000e+00, 5.0000e+00, 5.0000e+00
5.3102e-01, 5.5466e-01, 4.6885e-01, 1.0066e-03, 5.8000e-01, 6.6667e-01
0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00, 9.9338e-01, 0.0000e+00
0.0000e+00, 0.0000e+00]), tensor ([5.]))
The data is divided into three parts: Dtr, Den and Dte,Dtr as the training set and Dte as the test set.
CNN model 1. Model building
The CNN model is built as follows:
Class CNN (nn.Module): def _ init__ (self): super (CNN, self). _ _ init__ () self.conv1d = nn.Conv1d (1,64, kernel_size=2) self.relu = nn.ReLU (inplace=True) self.Linear1 = nn.Linear (64 * 37,50) self.Linear2 = nn.Linear (50,1) def forward (self) X): X = self.conv1d (x) x = self.relu (x) x = x.view (- 1) x = self.Linear1 (x) x = self.relu (x) x = self.Linear2 (x) return x
The convolution layer is defined as follows:
Self.conv1d = nn.Conv1d (1,64, kernel_size=2)
The original definition of one-dimensional convolution is:
Nn.Conv1d (in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True)
Here, the concept of channel is equivalent to embedding in natural language processing, where the number of input channels is 1, which means that the vector dimension size of each wind speed data is 1, the output channel is set to 64, and the convolution kernel size is 2.
The dimension of the original data is 38, that is, wind speed in the first 24 hours + 14 kinds of meteorological data. The size of the convolution kernel is 2. According to the previous formula, the dimensions of the original time series data after convolution are:
38-2 + 1 = 37
One-dimensional convolution is followed by an ReLU activation function:
Self.relu = nn.ReLU (inplace=True)
Then there are two fully connected layers:
Self.Linear1 = nn.Linear (64 * 37,50) self.Linear2 = nn.Linear (50,1)
The final output dimension is 1, that is, the wind speed we need to predict.
two。 Model training def CNN_train (): Dtr, Den, Dte = nn_seq () print (Dte [0]) epochs = 100model = CNN () .to (device) loss_function = nn.MSELoss () .to (device) optimizer = torch.optim.Adam (model.parameters ()) Lr=0.001) # training print (len (Dtr)) Dtr = Dtr [0Dtr 5000] for epoch in range (epochs): cnt = 0 for seq, y_train in Dtr: cnt = cnt + 1 seq, y_train = seq.to (device) Y_train.to (device) # print (seq.size ()) # print (y_train.size ()) # gradient zeroing before each parameter update and initializing optimizer.zero_grad () # Note here to reshape the sample # input size (batch size, channel, series length) y_pred = model (seq.reshape (1,1,-1)) loss = loss_function (y_pred) converted to conv1d Y_train) loss.backward () optimizer.step () if cnt% 500= = 0: print (f'epoch: {epoch:3} loss: {loss.item (): 10.8f}') print (f'epoch: {epoch:3} loss: {loss.item (): 10.10f}') state = {'model': model.state_dict () 'optimizer': optimizer.state_dict ()} torch.save (state,' Barcelona' + CNN_PATH)
A total of 100 rounds of training:
3. Model prediction and performance def CNN_predict (cnn, test_seq): pred = [] for seq, labels in test_seq: seq = seq.to (device) with torch.no_grad (): pred.append (cnn (seq.reshape (1,1,-1)). Item () pred = np.array ([pred]) return pred
Test:
Def test (): Dtr, Den, Dte = nn_seq () cnn = CNN (). To (device) cnn.load_state_dict (torch.load ('Barcelona' + CNN_PATH) [' model']) cnn.eval () pred = CNN_predict (cnn, Dte) print (mean_absolute_error (te_y, pred2.T), np.sqrt (mean_squared_error (te_y, pred2.T)
The performance of CNN on Dte is shown in the following table:
MAERMSE1.081.51
Thank you for reading this article carefully. I hope the article "how to use PyTorch to build CNN to achieve wind speed prediction" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.