SyntaxError: импорт * разрешен только на уровне модуля для systemml - PullRequest
0 голосов
/ 09 октября 2019

Я пытаюсь импортировать класс Keras2DML из библиотеки systemml, но получаю эту синтаксическую ошибку. Кто-нибудь знает, как это исправить? Я не внутри функции при вызове этого импорта. Текущий код:

from pyspark import SparkContext, SparkConf

import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler

from keras.layers import Dropout, LSTM, Dense, Input, Flatten
from keras.models import Model

from systemml.mllearn import *

class TrainLSTMModel:

    def __init__(self,  csv_file = None, column_number = None, timesteps = 30 ):
        self.csv_file = csv_file
        self.column_number = column_number
        self.timesteps = timesteps
        self.train_LSTM_model()


    def train_LSTM_model(self):
        '''This method will return a trained LSTM model based on the csv_file based in for training'''
        # Spark Session
        #conf = SparkConf().setAppName('Spark DL Pipeline').setMaster('local[8]')
        #sc = SparkContext(conf=conf)

        dataframe = pd.read_csv(self.csv_file)
        # Grab the desired column of the training data
        train_data = dataframe.iloc[:,self.column_number].values 
        # Reshaping to a 2D array
        train_data = train_data.reshape(-1,1)
        print(train_data.dtype)
        print(type(train_data))
        print(train_data.shape) 

        # Feature Scaling
        sc = MinMaxScaler(feature_range=(0, 1))
        scaled_train_data =sc.fit_transform(train_data)

        # Initialzing each x_train and y_train datasets for each column
        X_train = []
        y_train = []

        # Appending scaled training data to each dataset
        for i in range(self.timesteps, len(train_data)):
            X_train.append(scaled_train_data[i - self.timesteps:i, 0])
            y_train.append(scaled_train_data[i, 0])

        # Numpy array creation, Keras requires numpy arrays for Inputs
        X_train, y_train = np.array(X_train, dtype=int), np.array(y_train)
        print(X_train.shape)
        print(X_train.dtype)

        # Reshaping to a 3D matrix (970, 30, 1)
        #X_train = np.reshape(X_train, (X_train[0], X_train[1], 1))
        print(X_train.shape)


        # Reshapes to input neuron
        input_train_model = Input(shape =  (X_train.shape[1], 1), name='input_train_model')

        #Training Layers
        x_1 = LSTM(units=50, return_sequences=True, input_shape=(X_train.shape[1], 1))(input_train_model)
        x_1 = Dropout(0.2)(x_1)
        x_1 = LSTM(units = 50, return_sequences = True)(x_1)
        x_1 = Dropout(0.2)(x_1)
        x_1 = LSTM(units = 50, return_sequences = True)(x_1)
        x_1 = Dropout(0.2)(x_1)
        x_1 = LSTM(units = 50, return_sequences = True)(x_1)
        x_1 = Dropout(0.2)(x_1)
        x_1 = Flatten()(x_1)

        # 1 ouptut neuron for each column prediction
        output_train_data = Dense(units=1, name= 'ouput_train_data')(x_1)
        model = Model(inputs=input_train_model, outputs=output_train_data)
        model.compile(optimizer = 'adam', loss = 'mean_squared_error', metrics=['accuracy'])

        epochs = 5
        batch_size = 100
        samples = 60000
        max_iter = int(epochs*math.ceil(samples/batch_size))
        sysml_model = Keras2DML(spark, keras_model, input_shape=(1,28,28), weights='weights_dir', batch_size=batch_size, max_iter=max_iter, test_interval=0, display=10)
        #return model

Ошибка в ноутбуках Jupyter:

File "/Users/vnovelo/anaconda3/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3325, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)

  File "<ipython-input-58-2e205cfea32b>", line 12, in <module>
    from systemml.mllearn import Keras2DML

  File "/Users/vnovelo/anaconda3/lib/python3.7/site-packages/systemml/mllearn/__init__.py", line 45, in <module>
    from .estimators import *



    File "/Users/vnovelo/anaconda3/lib/python3.7/site-packages/systemml/mllearn/estimators.py", line 917
    def __init__(self, sparkSession, keras_model, input_shape, transferUsingDF=False, load_keras_weights=True, weights=None, labels=None, batch_size=64, max_iter=2000, test_iter=10, test_interval=500, display=100, lr_policy="step", weight_decay=5e-4, regularization_type="L2"):
    ^
SyntaxError: import * only allowed at module level`

Выход в VSCode:

Using TensorFlow backend.
Traceback (most recent call last):
  File "stats_app_systemml.py", line 10, in <module>
    from systemml.mllearn import *
  File "/Users/vnovelo/anaconda3/lib/python3.7/site-packages/systemml/mllearn/__init__.py", line 45, in <module>
    from .estimators import *
  File "/Users/vnovelo/anaconda3/lib/python3.7/site-packages/systemml/mllearn/estimators.py", line 917
    def __init__(self, sparkSession, keras_model, input_shape, transferUsingDF=False, load_keras_weights=True, weights=None, labels=None, batch_size=64, max_iter=2000, test_iter=10, test_interval=500, display=100, lr_policy="step", weight_decay=5e-4, regularization_type="L2"):
    ^
SyntaxError
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...