def transform_y(y_train): # Transform y_train. y_encoder = OneHotEncoder() y_encoder.fit(y_train) y_train = y_encoder.transform(y_train) return y_train, y_encoder if __name__ == '__main__': (x_train, y_train), (x_test, y_test) = mnist.load_data() x_train = np.squeeze(x_train.reshape((x_train.shape[0], -1))) x_test = np.squeeze(x_test.reshape((x_test.shape[0], -1))) y_train, y_encoder = transform_y(y_train) y_test, _ = transform_y(y_test) mlpModule = MlpModule(loss=classification_loss, metric=Accuracy, searcher_args={}, verbose=True) # specify the fit args data_transformer = DataTransformerMlp(x_train) train_data = data_transformer.transform_train(x_train, y_train) test_data = data_transformer.transform_test(x_test, y_test) fit_args = { "n_output_node": y_encoder.n_classes, "input_shape": x_train.shape, "train_data": train_data, "test_data": test_data } mlpModule.fit(n_output_node=fit_args.get("n_output_node"), input_shape=fit_args.get("input_shape"), train_data=fit_args.get("train_data"), test_data=fit_args.get("test_data"), time_limit=24 * 60 * 60)
def transform_y(y_train): # Transform y_train. y_encoder = OneHotEncoder() y_encoder.fit(y_train) y_train = y_encoder.transform(y_train) return y_train, y_encoder if __name__ == '__main__': (x_train, y_train), (x_test, y_test) = mnist.load_data() x_train = np.squeeze(x_train.reshape((x_train.shape[0], -1))) x_test = np.squeeze(x_test.reshape((x_test.shape[0], -1))) y_train, y_encoder = transform_y(y_train) y_test, _ = transform_y(y_test) mlp_module = MlpModule(loss=classification_loss, metric=Accuracy, searcher_args={}, verbose=True) # specify the fit args data_transformer = DataTransformerMlp(x_train) train_data = data_transformer.transform_train(x_train, y_train) test_data = data_transformer.transform_test(x_test, y_test) fit_args = { "n_output_node": y_encoder.n_classes, "input_shape": x_train.shape, "train_data": train_data, "test_data": test_data } mlp_module.fit(n_output_node=fit_args.get("n_output_node"), input_shape=fit_args.get("input_shape"), train_data=fit_args.get("train_data"), test_data=fit_args.get("test_data"),
xtrains = np.zeros((m-1,n)) ytrains = np.zeros((m-1,n-2)) for i in range(n): xtrains[:,i] = (2.0*xtrain[:,i]-(xmax[i]+xmin[i]))/(xmax[i]-xmin[i]) for i in range(n-2): ytrains[:,i] = (2.0*ytrain[:,i]-(ymax[i]+ymin[i]))/(ymax[i]-ymin[i]) #%% indices = np.random.randint(0,xtrains.shape[0],1000) xtrainv = xtrains[indices] ytrainv = ytrains[indices] #%% mlpModule = MlpModule(loss=regression_loss, metric=Accuracy, searcher_args={}, verbose=True) data_transformer = DataTransformerMlp(xtrains) train_data = data_transformer.transform_train(xtrains, ytrains) test_data = data_transformer.transform_test(xtrainv, ytrainv) fit_args = { "n_output_node": ytrains.shape[1], "input_shape": xtrains.shape, "train_data": train_data, "test_data": test_data } mlpModule.fit(n_output_node=fit_args.get("n_output_node"), input_shape=fit_args.get("input_shape"), train_data=fit_args.get("train_data"), test_data=fit_args.get("test_data"), time_limit=1 * 60* 60)
def train_autokeras(classes, alldata, labels, mtype, jsonfile, problemtype, default_features): ## this is a CNN architecture modelname = jsonfile[0:-5] + '_autokeras_%s' % (default_features) TEST_FOLDER = modelname x_train, x_test, y_train, y_test = train_test_split(alldata, labels, train_size=0.750, test_size=0.250) # we have to do some odd re-shapes to get the data loader to work for the autokeras module (keep this in mind when loading new data in) x_train = x_train.reshape(x_train.shape + (1, )) y_train = y_train.reshape(y_train.shape + (1, ) + (1, )) x_test = x_test.reshape(x_test.shape + (1, )) y_test = y_test.reshape(y_test.shape + (1, ) + (1, )) print(x_train.shape) print(y_train.shape) tensor_x = torch.stack([torch.Tensor(i) for i in x_train]) # transform to torch tensors tensor_y = torch.stack([torch.Tensor(i) for i in y_train]) my_dataset = utils.TensorDataset(tensor_x, tensor_y) # create your datset training_data = utils.DataLoader(my_dataset) # create your dataloader tensor_x = torch.stack([torch.Tensor(i) for i in x_test]) # transform to torch tensors tensor_y = torch.stack([torch.Tensor(i) for i in y_test]) my_dataset = utils.TensorDataset(tensor_x, tensor_y) # create your datset test_data = utils.DataLoader(my_dataset) # create your dataloader print(test_data) input_shape = x_train[0].shape n_output_node = 1 # cnnModule = CnnModule(loss=classification_loss, metric=Accuracy, searcher_args={}, path=TEST_FOLDER, verbose=False) if mtype == 'c': # metric = Accuracy is for classification # loss = classiciation_loss for classification mlpModule = MlpModule(loss=classification_loss, metric=Accuracy, searcher_args={}, path=TEST_FOLDER, verbose=True) elif mtype == 'r': # metric = MSE for regression # loss = regression_loss for regression mlpModule = MlpModule(loss=regression_loss, metric=MSE, searcher_args={}, path=TEST_FOLDER, verbose=True) timelimit = 60 print('training MLP model for %s hours' % (timelimit / (60 * 60))) mlpModule.fit(n_output_node, input_shape, training_data, test_data, time_limit=timelimit) mlpModule.final_fit(training_data, test_data, trainer_args=None, retrain=False) # # serialize model to JSON # mlpModule.export_autokeras_model(modelname+'.pickle') # print("\n Saved %s.pickle model to disk"%(modelname)) # # test opening model and making predictions # model=pickle_from_file(modelname+'.pickle') # results=model.evaluate(x_test, y_test) # print(results) cur_dir2 = os.getcwd() try: os.chdir(problemtype + '_models') except: os.mkdir(problemtype + '_models') os.chdir(problemtype + '_models') # now move all the files over to proper model directory shutil.copytree(cur_dir2 + '/' + TEST_FOLDER, os.getcwd() + '/' + TEST_FOLDER) shutil.rmtree(cur_dir2 + '/' + TEST_FOLDER)
import numpy as np from autokeras import MlpModule from autokeras.nn.loss_function import classification_loss from autokeras.nn.metric import Accuracy X_train = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) # 异或训练集 y_train = np.array([0, 1, 1, 0]) X_test = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) # 异或训练集 y_test = np.array([0, 1, 1, 0]) mlpModule = MlpModule(loss=classification_loss, metric=Accuracy, searcher_args={}, verbose=True) # 多层神经网络模型 mlpModule.fit( n_output_node=2, # 输出分几类 input_shape=(4, 2), # 输入的形状 train_data=X_train, # 训练集 test_data=y_train, # 验证集 time_limit=24 * 60 * 60) # 超时时间的设置