def test_model_with_softmax(): from models import Sequential from layers import Linear, Softmax inputs = np.array([[0.25, 0.63, 0.12]]) targets = np.array([0, 1, 0]) model = Sequential() model.add(Linear(3, 3, activation=Softmax())) predictions = model.feed_forward(inputs) loss = ce.loss(predictions, targets) for i in range(len(predictions)): gradient = ce.backward(predictions[i], targets[i]) print("grad", gradient)
def load_model(name): type_map = { "<class 'layers.SimpleRecurrent'>": SimpleRecurrent, "<class 'layers.VanillaRecurrent'>": VanillaRecurrent, "<class 'layers.Dense'>": Dense, "<class 'layers.Activation'>": Activation, "<class 'layers.Softmax'>": Softmax } with open('{}_network.json'.format(name), 'r') as infile: network = json.load(infile) shallow_params_dict = np.load('{}.npz'.format(name)) params_dict = {} for k, v in shallow_params_dict.items(): sep_ind = k.find('__') layer_ind = int(k[k.find('_') + 1:sep_ind]) param_key = k[sep_ind + 2:] if layer_ind not in params_dict: params_dict[layer_ind] = {} params_dict[layer_ind][param_key] = v model = Sequential() layer_types = network['layer_types'] layer_configs = network['layer_configs'] for i in range(len(layer_types)): lt = type_map[layer_types[i]] config = layer_configs[i] layer = lt(**config) if layer.trainable: layer.set_params_from_dict(params_dict[i]) model.add(layer) return model
def main(): (x_train, y_train), (x_test, y_test) = mnist.load_data() print 'Imported MNIST data: training input %s and training labels %s.' % ( x_train.shape, y_train.shape) print 'Imported MNIST data: test input %s and test labels %s.' % ( x_test.shape, y_test.shape) N, H, W = x_train.shape x = x_train.reshape((N, H * W)).astype('float') / 255 y = to_categorical(y_train, num_classes=10) model = Sequential() model.add(Dense(), ReLU(), layer_dim=(28 * 28, 300), weight_scale=1e-2) model.add(Dense(), ReLU(), layer_dim=(300, 100), weight_scale=1e-2) model.add(Dense(), Softmax(), layer_dim=(100, 10), weight_scale=1e-2) model.compile(optimizer=GradientDescent(learning_rate=1e-2), loss_func=categorical_cross_entropy) model.fit(x, y, epochs=10, batch_size=50, verbose=False) N, H, W = x_test.shape x = x_test.reshape((N, H * W)).astype('float') / 255 y = to_categorical(y_test, num_classes=10) model.evaluate(x, y)
y = y[indices] # Explicitly set apart 10% for validation data that we never train over split_at = len(X) - len(X) / 10 (X_train, X_val) = (slice_X(X, 0, split_at), slice_X(X, split_at)) (y_train, y_val) = (y[:split_at], y[split_at:]) print(X_train.shape) print(y_train.shape) print("Build model...") model = Sequential() # "Encode" the input sequence using an RNN, producing an output of HIDDEN_SIZE # note: in a situation where your input sequences have a variable length, # use input_shape=(None, nb_feature). model.add(RNN(HIDDEN_SIZE, input_shape=(MAXLEN, convertor.get_dim()))) # For the decoder's input, we repeat the encoded input for each time step model.add(RepeatVector(DIGITS + 1)) # The decoder RNN could be multiple layers stacked or a single keras_layer for _ in range(LAYERS): model.add(RNN(HIDDEN_SIZE, return_sequences=True)) # For each of step of the output sequence, decide which character should be chosen model.add(TimeDistributedDense(convertor.get_dim())) model.add(Activation("softmax")) model.compile(loss="categorical_crossentropy", optimizer="adam") # Train the model each generation and show predictions against the validation dataset for iteration in range(1, 200): print()
img_rows = 28 img_cols = 28 input_shape = (1, img_rows, img_cols) (train_x, train_y), (test_x, test_y) = mnist.load_data() train_x = np.reshape(train_x, (len(train_x), 1, img_rows, img_cols)).astype(skml_config.config.i_type) train_y = convert_to_one_hot(train_y, num_classes) test_x = np.reshape(test_x, (len(test_x), 1, img_rows, img_cols)).astype(skml_config.config.i_type) test_y = convert_to_one_hot(test_y, num_classes) train_x, valid_x, train_y, valid_y = train_test_split(train_x, train_y) filters = 64 model = Sequential() model.add(Convolution(filters, 3, input_shape=input_shape)) model.add(BatchNormalization()) model.add(ReLU()) model.add(MaxPooling(2)) model.add(Convolution(filters, 3)) model.add(BatchNormalization()) model.add(ReLU()) model.add(GlobalAveragePooling()) model.add(Affine(num_classes)) model.compile(SoftmaxCrossEntropy(), Adam()) train_batch_size = 100 valid_batch_size = 1 print("訓練開始: {}".format(datetime.now().strftime("%Y/%m/%d %H:%M"))) model.fit(train_x, train_y, train_batch_size, 20, validation_data=(valid_batch_size, valid_x, valid_y), validation_steps=1) print("訓練終了: {}".format(datetime.now().strftime("%Y/%m/%d %H:%M")))
from models import Sequential, compute_loss from layers import Layer min_loss = 100 best_model = None EPOCHS = 12 from random import uniform while min_loss > 6.25: model = Sequential() first_layer = Layer(4, "sigmoid") model.add(first_layer) second_layer = Layer(5, "sigmoid") model.add(second_layer) third_layer = Layer(4, "softmax") model.add(third_layer) model.compile() loss = 0 for i in range(EPOCHS): inpt = (uniform(-1, 1), uniform(-1, 1), uniform(-1, 1), uniform(-1, 1)) expected_output = [1 if n == max(inpt) else 0 for n in inpt] output = model.run(inpt) loss += compute_loss(output, expected_output) if loss < min_loss: best_model = model min_loss = loss print("Loss is: " + str(loss))
bias_weights_0 = np.array([-5, 5]) kernel_weights_0 = np.array([[5, -5], [5, -5]]) bias_weights_1 = np.array([-5]) kernel_weights_1 = np.array([[5], [5]]) saved_bias_0 = saved_weights(bias_weights_0) saved_kernel_0 = saved_weights(kernel_weights_0) saved_bias_1 = saved_weights(bias_weights_1) saved_kernel_1 = saved_weights(kernel_weights_1) model = Sequential() model.add(Dense(2, 2, kernel_initializer=saved_kernel_0, bias_initializer=saved_bias_0, alpha=50.0)) model.add(Sigmoid()) model.add(Dense(1, 2, kernel_initializer=saved_kernel_1, bias_initializer=saved_bias_1, alpha=50.0)) model.add(Sigmoid()) X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) y = np.array([[1], [0], [0], [1]]) print("Prediction") p = model.predict(X)
from layers import Dense from layers import Sigmoid from models import Sequential from initializers import saved_weights from metrics import SquaredError import matplotlib.pyplot as plt kernel_weights = np.array([[0, 0], [0, 0]]) bias_weights = np.array([0, 0]) saved_kernel = saved_weights(kernel_weights) saved_bias = saved_weights(bias_weights) model = Sequential() model.add(Dense(2, 2, kernel_initializer=saved_kernel, bias_initializer=saved_bias, alpha=0.5)) model.add(Sigmoid()) X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) y = np.array([[0, 1], [1, 0], [1, 0], [1, 0]]) print("Prediction") p = model.predict(X) print(p) print("Error")
else: sys.stdout.write('ETA: ' + seconds_to_string(remaining)) # Output padding sys.stdout.write(' ' * 20) # Allow progress bar to persist if it's complete if current == total: sys.stdout.write('\n') # Flush to standard out sys.stdout.flush() # Return the time of the progress update return time.time() model = Sequential() model.add(Input(2)) model.add(Dense(25)) model.add(Activation("relu")) model.add(Dense(50)) model.add(Activation("relu")) model.add(Dense(50)) model.add(Activation("relu")) model.add(Dense(25)) model.add(Activation("relu")) model.add(Dense(1)) model.add(Activation("sigmoid")) def initialise_layer_parameters(seed=2): # random seed initiation np.random.seed(seed)
""" mndata = MNIST('./samples') images, labels = mndata.load_training() vocab = set() for label in labels: vocab.add(label) vocab = sorted(vocab) Y = [] for label in labels: one_hot = [0] * len(vocab) one_hot[label] = 1 Y.append(one_hot) X = np.array(images).T / 255 Y = np.array(Y).T return (X, Y) X, Y = parse_data() model = Sequential() model.add(Dense(1024, n_inputs=X.shape[0])) model.add(Dense(1024)) model.add(Dense(1024)) model.add(Dense(Y.shape[0], activation='sigmoid')) model.compile() # model = pickle.load(open('model.p', 'rb')) model.fit(X, Y, 1, learning_rate=0.003)
num_labels = 10 image_pixels = image_size**2 with open("pickled_mnist.pkl", "br") as fh: data = pickle.load(fh) train_imgs = data[0] test_imgs = data[1] train_labels = data[2] test_labels = data[3] train_labels_one_hot = data[4] test_labels_one_hot = data[5] model = Sequential() model.add( Dense(16, 784, kernel_initializer=truncated_normal, bias_initializer=zeros)) model.add(Sigmoid()) model.add( Dense(10, 16, kernel_initializer=truncated_normal, bias_initializer=zeros)) model.add(Sigmoid()) loss = SquaredError() loss_history = model.fit(train_imgs, train_labels_one_hot, batch_size=32, epochs=10, loss=loss, halt=False) pred = model.predict(test_imgs)
return x, x.shape[1], t, 1 #irisからデータを生成 #x, inputs_shape, t, outputs_shape = create_data_category() #loss = "categorical_crossentropy" #metric = "accuracy" #last_layer_activation = "softmax" x, inputs_shape, t, outputs_shape = create_data_numeric(3) loss = "mean_squared_error" metric = "rmse" last_layer_activation = "identify" seed = 15 model = Sequential(seed=seed) model.add(Dense(10, activation="relu", inputs_shape=inputs_shape)) model.add(Dense(10, activation="relu")) model.add(Dense(outputs_shape, activation=last_layer_activation)) model.compile(loss=loss, optimizer=Adam(), metric=metric) train_x, test_x, train_t, test_t = train_test_split(x, t, test_size=0.3, random_state=seed) model.fit(train_x, train_t, test_x, test_t, epochs=1000, batch_size=50) #誤差をプロット import matplotlib.pyplot as plt plt.plot(model.history_train[0]) plt.plot(model.history_test[0])
(x_train, y_train), (x_test, y_test) = mnist.load_data() x_train = (x_train.astype('float32') / 255).reshape(-1, 1, 28, 28) y_train = np_utils.to_categorical(y_train.astype('int32'), 10) x_test = (x_test.astype('float32') / 255).reshape(-1, 1, 28, 28) y_test = np_utils.to_categorical(y_test.astype('int32'), 10) threshold = 1000 x_train = x_train[:threshold] y_train = y_train[:threshold] x_test = x_test[:threshold] y_test = y_test[:threshold] seed = 15 model = Sequential(seed=seed) model.add(Conv2D(32, (5, 5), activation="relu", inputs_shape=x_train.shape[1:])) model.add(Pooling((2, 2))) model.add(Conv2D(16, (3, 3), activation="relu")) model.add(Pooling((2, 2))) model.add(Dense(10, activation="relu")) model.add(Dropout(0.5)) model.add(Dense(10, activation="softmax")) model.compile(loss="categorical_crossentropy", optimizer=Adam(), metric="accuracy") model.fit(x_train=x_train, t_train=y_train, x_test=x_test, t_test=y_test, batch_size=128,
num_episodes = 1000 num_steps = 200 success_steps = 100 reward_fail = -1 reward_success = 1 reward_none = 0 env = gym.make("CartPole-v0") env = wrappers.Monitor(env, "videos", (lambda ep: ep % 100 == 0), True) num_actions = env.action_space.n num_observations = env.observation_space.shape[0] input_shape = (num_observations, ) hidden_size = 32 model = Sequential() model.add(Affine(hidden_size, input_shape=input_shape)) model.add(ReLU()) model.add(Affine(hidden_size)) model.add(ReLU()) model.add(Affine(hidden_size)) model.add(ReLU()) model.add(Affine(num_actions)) model.compile(MeanSquaredError(), Adam()) explorer = LinearDecayEpsilonGreedy(1.0, 0.0, 500, env.action_space.sample) replay_buffer = ReplayBuffer(10**6) agent = DQN(model, replay_buffer, explorer, sync_target_interval=5, replay_size=32,