ModuleAttributeError: 'RNN' object has no attribute 'scalerX'

Hi I'm currently want to integrate a python module in R. basically I want to call the main command in R, but all Classes and Function are written in python. I'm using reticulate in R and when I'm calling a function plot_result in my RNN class it shows an error that there is no attribute 'scalerX' in RNN

when I run the python code with the exact same commands it works just fine.
Here are my codes in R and python: you can see the error message in attachment.


Please help me with this issues, I reallyneed it for my thesis.

rnn <- import("demo_lorenz_RNN", as = NULL, convert = TRUE, delay_load = FALSE)

rnn$plt$style$use('seaborn')
data <- rnn$getLorenzData(1000L,200L,"np",TRUE)
data <- as.data.frame(data)

x_train <- data[1:3]
x <- x_train[1:2]
y <- x_train[3]
x <- data.matrix(x[1:2])
y <- data.matrix(y[1])
input_names <- list('x_0', 'x_1', 'x_2')
output_names <- list('x_2')

model <- rnn$RNN(epochs=100L,
                 nHorizon=0L,
                 RNNType="gru",  # "lstm"
                 opt_type='ADAMW',
                 output_names=output_names,
                 input_names=input_names,
                 nWindow=50L,  # 24 * 7
                 nHighwayWindow=12L,
                 dropout=0.1,
                 batch_size=64L,
                 hidden_size=20L,
                 lr=0.001,
                 use_cuda=TRUE,
                 verbose=2L,
                 ScalerTypeX="StandardScaler"

rnn$model$fit(x,y)
rnn$model$summary()
rnn$plot_inputs(model, x, showlegend=TRUE, names=NULL)
rnn$plot_result(model, x, y, nTrain=500L, plotdelta=FALSE, showlegend=TRUE, names=list("x_2"))
rnn$plt$show()

here are only snippet codes in python I hope you could still understand it: Note that def predict(), def fit() , and def fit_ADAMW() are in the function in RNN class, but def plot_result are outside the RNN class

through the code rnn$model$fit(x,y), the variable "scalerX", should already be defined(MinMaxScaler or StandardScaler) as you can see in the def fit_ADAMW()
but somehow the R couldn't read the variable "scalerX", or maybe r cant save the variable which already be defined before. That's why in "rnn$plot_result()" shows an error.

def plot_result(model, x, y, nTrain, plotdelta=True, showlegend=True, names=None):
    plt.figure()
    N = x.shape[0]
    nTrain = min(N, nTrain)
    print(f'nTrain = {nTrain}')
    target_all = y
    y_star = model.predict(x)
...
...

def predict(self, x):
        N = x.shape[0]
        #print(f'RNN predict N = {N}')
        ret = np.empty((N, 1))
        ret[:] = np.nan

        x = self.scalerX.transform(x)
        x_star = torch.from_numpy(x).type(self.dtype).to(self.device)
        #print(f'x_star.shape = {x_star.shape}')
        # bc=N, nWindow, dim
        input_x = x_star.view(1, x_star.shape[0], x_star.shape[1])
        #print(f'input_x.shape = {input_x.shape}')
        idy = range(self.nHorizon, x.shape[0])  # -self.nHorizon)
        #print(f'RNN predict idy = {idy}')
        y_star = self.forward(input_x)

        #print(f'RNN 1 y_star.shape = {y_star.shape}')
        y_star = y_star.view(1, -1)
        #print(f'RNN 2 y_star.shape = {y_star.shape}')
        y_star = y_star.data.cpu().numpy().transpose()
        #print(f'RNN 3 y_star.shape = {y_star.shape}')
        y_star = self.scalerY.inverse_transform(y_star)
        if self.nHorizon > 0:
            y_star = y_star[:-self.nHorizon]
        #print(f'RNN 3 y_star.shape = {y_star.shape}')
        ret[idy, :] = y_star
        #print(f'RNN predict y_star.shape = {y_star.shape}')
        return ret
...
...

def fit(self, x, y):
        print(f'self.opt_type = {self.opt_type}')
        if self.opt_type == 'ADAMW':
            self.fit_ADAMW(x, y)
        elif self.opt_type == 'GPMINIMIZE':
            self.fit_GPMINIMIZE(X, Y)
        elif self.opt_type == 'LSQ':
            self.fit_LSQ(x, y)
        else:
            raise NotImplementedError()

def fit_ADAMW(self, x, y):
        self.input_size = x.shape[1]
        if self.LossType == 'L1':
            self.criterion = torch.nn.L1Loss(size_average=False)
        elif self.LossType == 'MSE':
            self.criterion = torch.nn.MSELoss(size_average=False)

        if self.use_cuda:
            self.cuda()
            self.criterion.cuda()
        else:
            self.cpu()
        self.input_size = x.shape[1]
        self.P = TSDataHandler(nWindow=self.nWindow,
                               nHorizon=self.nHorizon,
                               nSeq=self.nWindow)
        #print(f'nWindow={self.nWindow}, nHorizon={self.nHorizon}, nSeq={self.nWindow}')

        if self.ScalerTypeX == 'StandardScaler':
            self.scalerX = StandardScaler().fit(x)
        elif self.ScalerTypeX == 'MinMaxScaler':
            self.scalerX = MinMaxScaler().fit(x)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.