Sagemaker: Training everytime I need make a prediction: how should I structure the solution?
I have been asked to migrate a custom model to Sagemaker. This model is a forecasting script that trains everytime it is run and then predicts after training. (It is a two-layer forecasting prediction with SARIMAX). The flow is as explained below:
- train arima model to get exogenous variables (training algorithm 1)
- predict with that trained model
- use the output variables to train the second layer (training algorithm 2)
- predict with this last trained model and output the solution
This is not what im used to do in Sagemaker (I train a model once that will be invoked multiple times), so how could I frame this? Train models separately from two separate docker images and create two endpoints? The whole train-predict-train-predict workflow would no longer be automatic, right? How would I trigger this workflow? Please help!
See also questions close to this topic
Error while fitting data to the model: TypeError: 'NoneType' object is not callable
I am using this code for the generator:
train_generator = train_datagen.flow_from_directory("C:\\Users\\Rahul\\Desktop\\AI\\Audio\\Classifying music notes\\output\\train", batch_size = 20, class_mode = 'binary', target_size = (864, 432)) validation_generator = test_datagen.flow_from_directory( "C:\\Users\\Rahul\\Desktop\\AI\\Audio\\Classifying music notes\\output\\val", batch_size = 20, class_mode = 'binary', target_size = (864, 432))
I am getting this error when I run the below code: TypeError: 'NoneType' object is not callable
history = model.fit_generator( train_generator, validation_data = validation_generator, steps_per_epoch = 100, epochs = 100, validation_steps = 50, verbose = 2, callbacks=[callbacks])
Problems initializing model in pytorch
I can't initialize my model in pytorch and get:
TypeError Traceback (most recent call last) <ipython-input-82-9bfee30a439d> in <module>() 288 dataset = News_Dataset(true_path=args.true_news_file, fake_path=args.fake_news_file, 289 embeddings_path=args.embeddings_file) --> 290 classifier = News_classifier_resnet_based().cuda() 291 try: 292 classifier.load_state_dict(torch.load(args.model_state_file)) /usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs) 548 result = self._slow_forward(*input, **kwargs) 549 else: --> 550 result = self.forward(*input, **kwargs) 551 for hook in self._forward_hooks.values(): 552 hook_result = hook(self, input, result) TypeError: forward() missing 1 required positional argument: 'input'
Someone asked for code. It is given below
class News_classifier_resnet_based(torch.nn.Module): def __init__(self): super().__init__() self.activation = torch.nn.ReLU6() self.sigmoid = torch.nn.Sigmoid() self.positional_encodings = PositionalEncoder() self.resnet = list(torch.hub.load('pytorch/vision:v0.6.0', 'resnet18', pretrained=True).children()) self.to_appropriate_shape = torch.nn.Conv2d(in_channels=1, out_channels=1, kernel_size=77) self.conv1 = torch.nn.Conv2d(in_channels=1,out_channels=64,kernel_size=7,stride=2,padding=3) self.conv1.weight = torch.nn.Parameter(self.resnet.weight[:,0,:,:].data) self.center = torch.nn.Sequential(*self.resnet[1:-2]) self.conv2 = torch.nn.Conv2d(in_channels=512, out_channels=1, kernel_size=1) self.conv3 = torch.nn.Conv2d(in_channels=1,out_channels=1,kernel_size=7) self.title_conv = torch.nn.Sequential( torch.nn.Conv2d(in_channels=1,out_channels=1,kernel_size=3,stride=3), self.activation(), torch.nn.Conv2d(in_channels=1,out_channels=1,kernel_size=2,stride=2), self.activation(), torch.nn.Conv2d(in_channels=1,out_channels=1,kernel_size=2,stride=2) ) self.title_lin = torch.nn.Linear(25,1) self.year_lin = torch.nn.Linear(10,1) self.month_lin = torch.nn.Linear(12,1) self.day_lin = torch.nn.Linear(31,1) self.date_lin = torch.nn.Linear(3,1) self.final_lin = torch.nn.Linear(3,1) def forward(self,x_in): #input shape - (batch_size, 3+title_len+seq_len, embedding_dim) #output shape - (batch_size, 1) year = x_in[:,0,:10] month = x_in[:,1,:12] day = x_in[:,2,:31] title = x_in[:,3:3+args.title_len,:] text = x_in[:,3+args.title_len:,:] title = self.positional_encodings(title) text = self.positional_encodings(text) text = text.unsqueeze(1) text = self.activation(self.to_appropriate_shape(text)) text = self.activation(self.conv1(text)) text = self.activation(self.center(text)) text = self.activation(self.conv2(text)) text = self.activation(self.conv3(text)) text = text.reshape(args.batch_size,-1) title = title.unsqueeze(1) title = self.activation(self.title_conv(title)) title = title.reshape(args.batch_size,-1) title = self.activation(self.title_lin(title)) year = self.activation(self.year_lin(year)) month = self.activation(self.month_lin(month)) day = self.activation(self.day_lin(day)) date = torch.cat([year,month,day], dim=-1) date = self.activation(self.date_lin(date)) final = torch.cat([date,title,text], dim=-1) final = self.sigmoid(self.final_lin(final)) return final classifier = News_classifier_resnet_based().cuda()
What should I do? StackOverflow asked for more details. I'm trying to classify texts using word embeddings but problem lies in last line. I am working in google colab. Also when I created some models in other code blocks, I've got no problems
CNN-LSTM Model on non-image dataset
I have a dataset in this form
name peoject_grade project_summary Cost_on_project Subject Topics Rating Wilcox 7 this project is.. 455$ Chemistry Atomic 4.2
Its shape is (51722,7)
I have been asked to apply cnn-lstm model on this dataset. How is it possible ?? Its not a image dataset like mnist so how can i apply cnn-lstm model here.
The basic code for cnn-lstm is like this -
model=Sequential() model.add(TimeDistributed(Conv1D(32, kernel_size=(3, 3), padding='same'), input_shape=(frames, 224, 224, 3))) model.add(TimeDistributed(Activation('relu'))) model.add(TimeDistributed(Conv1D(32, (3, 3)))) model.add(TimeDistributed(Activation('relu'))) model.add(TimeDistributed(MaxPooling2D(pool_size=(2, 2)))) model.add(TimeDistributed(Dropout(0.25))) model.add(TimeDistributed(Dense(512))) model.add(TimeDistributed(Flatten())) model.add(LSTM(20, return_sequences=True, name="lstm_layer_rgb")); model.add(TimeDistributed(Dense(num_classes), name="time_distr_dense_one_rgb")) model.add(GlobalAveragePooling1D(name="global_avg_rgb"))
But what input_size, kernal_size and other changes i need to do for this dataset ??
How to fit an ARIMA model with non-complete years data in R?
How i can fit
ARIMAmodel to the following filtered
Flowdata? that is fit the model using
January to July 10data for the chosen years (ie., 2012,2015,2017,2020) and then use the fitted model to
forecastJuly 11-August 31.
library(forecast) library(tidyverse) library(dplyr) set.seed(1500) FakeData <- data.frame(Date = seq(as.Date("2010-01-01"),to = as.Date("2020-07-10"), by = "day"), Flow = runif(length(Date), 25, 75)) %>% mutate( Year = year(Date), Month = month(Date), Day = day(Date)) %>% filter(Year %in% c(2012,2015,2017,2020)) %>% filter(between(Month, 1,7)) %>% filter(!c(Month == 07 & Day >= 11)) #convert to Timeseries data TsData <- ts(FakeData$Flow) # how to provide start and end date that only goes to up to July 10 of the filtered year? # find the best ARIMA model AA <- auto.arima(TsData) # fit and forecast with the model ModelFit <- forecast(AA, h = 51) # I want to forecast for July 11 to August 31 of 2020
Changing Training Period in Arima Model in R
Arima forecasts in R seem to change depending on the amount of data I input to the Arima() function. For example, I have a 30 days worth of sales data. If I input days 1-20 and forecast day 21, I get a different forecast than if I input days 10-20. This doesn't make sense to me because an Arima(p,d,q) model only looks at the previous p,q days data. So why would a forecast change if I change the amount of data I input into the function?
Forecasting using GRNN (Generalized Regression Neural Network) in Matlab
Does Anyone know? I have a problem when using GRNN (Generalized Regression Neural Network) in Matlab. I use this neural network for forecasting of energy consumption. I have data about energy consumption from 1990-2015. The trend is going up. The most recent year is, the highest score is,. The pattern is 1 output and 3 input. when I predict for 2016, 2017, 2018, the value should always go up from before. But the all 3 latest year have same value of energy consumption with 2015. Can anyone solve this problem?
Updating Sagemaker Endpoint with new Endpoint Configuration
A bit confused with automatisation of Sagemaker retraining the model.
Currently I have a notebook instance with Sagemaker
LinearLernermodel making the classification task. So using
EstimatorI'm making training, then deploying the model creating
Endpoint. Afterwards using
Lambdafunction for invoke this endpoint, I add it to the
API Gatewayreceiving the api endpoint which can be used for POST requests and sending back response with class.
Now I'm facing with the problem of retraining. For that I use
lambdafunction getting environment variables for training_jobs. But the problem that Sagemaker not allow to rewrite training job and you can only create new one. My goal is to automatise the part when the new training job and the new endpoint config will apply to the existing endpoint that I don't need to change anything in API gateway. Is that somehow possible to automatically attach new endpoint config with existing endpoint?
what AWS SageMaker’s Built-in Algorithm would be the appropriate?
i have existing database of 100k pdf documents and metadata associate with each document. the MetaData consists of DocumentName,DocumentCategory,DocumentExpirationDate.
now i want to use an AWS SageMaker’s Built-in ML Algorithm to detect the Document metadata for new documents by training the SageMaker using existing data.
can you please suggest AWS SageMaker’s Built-in Algorithm that would be the appropriate to detect for each of the Document MetaData.
Is possible to change my sagemaker model to make predictions directly using raw (.wav) file?
I have a model wich accepts features extracted from a .wav file as json array to make predictions. I want to make predictions by sending the .wav file directly to the model. I do not want to add additional step for preprocessing the .wav file. Is it possible ? My algorithm is very much similar to this one.
Fitting ARIMA model for non-sequential dates in R?
I want to fit an ARIMA model for the
filteredyear and use it to
forecastFlow for the
July 11-August 31period for the year 2020. Any help would be appreciated.
library(forecast) library(tidyverse) library(lubridate) DF <- data.frame(Date = seq(as.Date("2010-01-01"),to = as.Date("2020-07-10"), by = "day"), Flow = runif(length(Date), 25, 75)) %>% mutate( Year = year(Date), Month = month(Date), Day = day(Date)) %>% filter(Year %in% c(2012,2015,2017,2020))