def create_optimizer(self,
                         optimizer="Adam",
                         learning_rate=0.01,
                         beta_1=0.9,
                         beta_2=0.999,
                         epsilon=1.e-7,
                         **opts):
        """

        Args:
            optimizer ():
            learning_rate ():
            beta_1 ():
            beta_2 ():
            epsilon ():
            **opts ():

        Returns:

        """

        assert isinstance(optimizer, str)

        if optimizer == "Adam":
            self._optimizer = Adam(learning_rate=learning_rate,
                                   beta_1=beta_1,
                                   beta_2=beta_2,
                                   epsilon=epsilon,
                                   amsgrad=False)
        elif optimizer == "Amsgrad":
            self._optimizer = Adam(learning_rate=learning_rate,
                                   beta_1=beta_1,
                                   beta_2=beta_2,
                                   epsilon=epsilon,
                                   amsgrad=True)
        elif optimizer == "Adamax":
            self._optimizer = Adamax(learning_rate=learning_rate,
                                     beta_1=beta_1,
                                     beta_2=beta_2,
                                     epsilon=epsilon)

        else:
            raise ValueError(
                "Unknown optimizer mode for AdamXManager: {}".format(
                    optimizer))
Exemple #2
0
    def __init__(self, name, alpha, gamma, input_layer_size, number_of_parameters, out_layer_size, memory_size=50000,
                 batch_size=64):

        config = configparser.ConfigParser()
        config.read('configuration/agent_config.ini')
        config.sections()

        enable_model_load = config['model_weights'].getboolean('enable_load_model_weights')
        self.enable_model_save = config['model_weights'].getboolean('enable_save_model_weights')
        self.tensorboard_visualization = config['tensorboard'].getboolean('enable_ddqnper')

        # Hyperparameters
        self.memory = MemoryBuffer(max_size=memory_size, number_of_parameters=input_layer_size, with_per=True)
        self.with_per = True
        self.gamma = gamma
        self.learning_rate = alpha
        self.batch_size = batch_size
        self.out_layer_size = out_layer_size
        self.replace_target_network_after = config['model_settings'].getint('ddqn_replace_network_interval')
        self.action_space = [i for i in range(out_layer_size)]
        self.priority_offset = 0.1  # used for priority, as we do not want to have priority 0 samples
        self.priority_scale = 0.7  # priority_scale, suggested by Paper

        loss = Huber()
        optimizer = Adam(learning_rate=alpha)

        # Epsilon Greedy Strategy
        self.epsilon = 1.0  # enable epsilon = 1.0 only when changing model, else learned weights from .h5 are used.
        self.epsilon_decay = 0.9985
        self.epsilon_min = 0.005

        # Keras Models
        hl1_dims = 128
        hl2_dims = 64
        hl3_dims = 64

        self.dqn_eval = self._build_model(hl1_dims, hl2_dims, hl3_dims, input_layer_size, out_layer_size, optimizer,
                                          loss)
        self.dqn_target = self._build_model(hl1_dims, hl2_dims, hl3_dims, input_layer_size, out_layer_size, optimizer,
                                            loss)
        # self.history = History()

        if self.tensorboard_visualization:
            comment = 'adam-huber-reward_per2'
            path = config['tensorboard']['file_path']
            tboard_name = '{}{}-cmt-{}_hl1_dims-{}_hl2_dims-{}_hl3_dims-{}-time-{}'.format(path, name, comment,
                                                                                           hl1_dims,
                                                                                           hl2_dims, hl3_dims,
                                                                                           int(time.time()))
            self.tensorboard = TensorBoard(tboard_name.format())

        self.keras_weights_filename = '{}.keras'.format(name)
        self.model_loaded = False
        if enable_model_load:
            self.load_model()
        else:
            print('Applying epsilon greedy strategy')
Exemple #3
0
def get_pen_cnn(input_data, num_labels):
    model = Sequential()
    model.add(Conv2D(64, kernel_size=(3, 3), activation='relu', input_shape=input_data.shape))
    model.add(MaxPooling2D(pool_size=2, strides=2))
    model.add(Conv2D(128, kernel_size=(3, 3), activation='relu'))
    model.add(Flatten())
    model.add(Dense(500, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(num_labels, activation='softmax'))

    opt = Adam(learning_rate=.0003)

    model.compile(loss='categorical_crossentropy', opt=opt, metrics=['accuracy'])
    return model
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dense(256, activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(BatchNormalization())
model.add(Dropout(0.5))

model.add(Dense(10, activation='softmax'))

checkpointer = tf.keras.callbacks.ModelCheckpoint(filepath="best_weights.hdf5",
                                                  monitor='val_accuracy',
                                                  verbose=1,
                                                  save_best_only=True)

opt = Adam(lr=LR, decay=LR / NUM_EPOCHS)

model.compile(optimizer=opt,
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])
history = model.fit(x_train[:, :, :, np.newaxis],
                    y_train,
                    epochs=NUM_EPOCHS,
                    batch_size=BATCH_SIZE,
                    callbacks=[checkpointer],
                    validation_split=0.2)

# model.save('cnn.h5')
# model.load_weights('best_weights.hdf5')
# Plot learning curve
        numberbatch_file_loc = 'retrogan/mini.h5'
        target_file_loc = tools.directory + tools.datasets["mine"][0]
        cleanup_vocabulary_nb_based(numberbatch_file_loc, target_file_loc)
        tools.datasets["mine"][0] += "clean"

    print("Dataset:", tools.datasets[dataset])
    plain_word_vector_path = plain_retrofit_vector_path = tools.directory
    plain_word_vector_path += tools.datasets[dataset][0]
    plain_retrofit_vector_path += tools.datasets[dataset][1]
    print("Loading the model!")
    # Load the model and init the weights
    to_retro_converter = load_model(
        trained_model_path,
        custom_objects={"ConstMultiplierLayer": ConstMultiplierLayer},
        compile=False)
    to_retro_converter.compile(optimizer=Adam(), loss=['mae'])
    to_retro_converter.load_weights(trained_model_path)

    # Generate retrogan embeddings
    print("Generating embeddings")
    retro_df = pandas.DataFrame()
    #
    word_embeddings = pd.read_hdf(plain_word_vector_path,
                                  'mat',
                                  encoding='utf-8').swapaxes(0, 1)
    # # word_embeddings = word_embeddings.loc[[x for x in word_embeddings.index if "." not in x]]
    vals = np.array(
        to_retro_converter.predict(np.array(word_embeddings.values).reshape(
            (-1, dimensionality)),
                                   batch_size=64))
    to_txt = True
    ax.set_title('Потери генератора (x) и дискриминатора (o)')
    ax.set_ylabel('Потери')
    ax.set_xlabel('Эпоха / 100')
    ax.set_xlim([-0.5, cnt])
    ax.set_ylim([0, 1.1 * yMax])
    fig.show()


pathToData = 'data//'
pathToHistory = 'history//'
img_rows = 64
img_cols = 64
channels = 1
num_classes = 6
img_shape = (img_rows, img_cols, channels)
optimizer = Adam(0.0002, 0.5)
loss = 'binary_crossentropy'
loss_g = 'binary_crossentropy'  # 'mse', 'poisson', 'binary_crossentropy'
# latent_dim - размер шума, подаваемого на вход генератора
# Шум - это вектор, формируемый на базе нормального распределения
# Число формируемых векторов равно batch_size
# Шум можно рассматривать как изображение размера 10*10
latent_dim = 100
epochs = 30001  # Число эпох обучения (30001)
batch_size = 30  # Размер пакета обучения (число генерируемых изображений)
sample_interval = 3000  # Интервал между сохранением сгенерированных изображений в файл

# Построение генератора
generator = build_generator(img_shape, latent_dim)
# Построение и компиляция дискриминатора
discriminator = build_discriminator(img_shape, loss, optimizer)
Exemple #7
0
def main():
    train_x = np.empty(0, dtype='uint8')
    train_y = np.empty(0, dtype='uint8')
    batch_size = 4

    x_dir = 'DataForSegmentator/input'
    y_dir = 'DataForSegmentator/output_filters'

    samples_num = len(os.listdir(x_dir))

    for x_path, y_path in zip(sorted(os.listdir(x_dir)),
                              sorted(os.listdir(y_dir))):
        train_x = np.append(train_x,
                            np.array(Image.open("%s/%s" % (x_dir, x_path))))
        train_y = np.append(train_y,
                            np.array(Image.open("%s/%s" % (y_dir, y_path))))

    train_x.shape = (samples_num, 256, 256, 3)
    train_y.shape = (samples_num, 256, 256, 3)

    if not os.path.isfile('train_unet_x.npy'):
        np.save('train_unet_x', train_x)
    train_x = np.memmap('train_unet_x.npy', shape=train_x.shape, offset=128)

    x_generator = ImageDataGenerator(
        shear_range=0.1,
        zoom_range=0.1,
        horizontal_flip=True,
        vertical_flip=True,

    ) \
        .flow(
        x=train_x,
        batch_size=batch_size,
        seed=42
    )
    y_generator = ImageDataGenerator(
        shear_range=0.1,
        zoom_range=0.1,
        horizontal_flip=True,
        vertical_flip=True,

    ) \
        .flow(
        x=train_y,
        batch_size=batch_size,
        seed=42
    )

    train_generator = zip(x_generator, y_generator)

    model = custom_unet(input_shape=train_x.shape[1:],
                        use_batch_norm=False,
                        num_classes=3,
                        filters=32,
                        dropout=0.2,
                        output_activation='relu')

    # model = get_full_model(json_path='models/model_unet_70.json', h5_path='models/model_unet_70.h5')

    model.compile(optimizer=Adam(lr=1e-8), loss='mae', metrics=['accuracy'])

    callbacks = [
        ModelCheckpoint("model_unet.h5",
                        monitor='acc',
                        verbose=True,
                        save_best_only=True),
        # EarlyStopping(monitor='acc',
        #               patience=0,
        #               baseline=90,
        #               verbose=True,
        #               ),
    ]

    full_history = {"acc": np.empty(0), "loss": np.empty(0)}

    continue_train = True
    epochs_sum = 0
    while continue_train:

        epochs = get_input_int("How many epochs?", 0, 100)

        history = model.fit_generator(
            generator=train_generator,
            steps_per_epoch=int(samples_num / batch_size),
            # validation_steps=train_x.shape[0] / batch_size,
            epochs=epochs,
            shuffle=True,
            verbose=True,
            callbacks=callbacks,
        )

        if epochs != 0:
            full_history['acc'] = np.append(full_history['acc'],
                                            history.history['accuracy'])
            full_history['loss'] = np.append(full_history['loss'],
                                             history.history['loss'])
            epochs_sum = len(full_history['acc'])

            #####################################################################
            # ----------------------- evaluate model ----------------------------
            #####################################################################
            print("\nacc        %.2f%%\n" %
                  (history.history['accuracy'][-1] * 100),
                  end='')

            epochs = len(history.history['accuracy'])

            plot_history_separate_from_dict(history_dict=full_history,
                                            save_path_acc=None,
                                            save_path_loss=None,
                                            show=True,
                                            save=False)

        print("epochs: %d - %d" % (epochs_sum - epochs, epochs_sum))

        #####################################################################
        # ----------------------- CMD UI ------------------------------------
        #####################################################################
        if get_stdin_answer("Show image of prediction?"):
            show_predict_on_window(
                train_x, train_y,
                np.array(model.predict(train_x), dtype='uint8'))

        if get_stdin_answer(text='Save model?'):
            save_model_to_json(model,
                               "models/model_unet_%d.json" % (epochs_sum))
            model.save_weights('models/model_unet_%d.h5' % (epochs_sum))

        continue_train = get_stdin_answer(text="Continue?")
encoded_string = encoder.encode(sample_string)
print('Encoded string is {}'.format(encoded_string))

original_string = encoder.decode(encoded_string)
print('The original string: "{}"'.format(original_string))

train_dataset, test_dataset = dataset['train'], dataset['test']
BUFFER_SIZE = 10000
BATCH_SIZE = 64
train_dataset = train_dataset.shuffle(BUFFER_SIZE)
train_dataset = train_dataset.padded_batch(BATCH_SIZE,
                                           train_dataset.output_shapes)

test_dataset = test_dataset.padded_batch(BATCH_SIZE,
                                         test_dataset.output_shapes)

model = tf.keras.Sequential([
    tf.keras.layers.Embedding(encoder.vocab_size + 2, 1000),
    tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(1000)),
    tf.keras.layers.Dense(1000, activation='relu'),
    tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(loss='binary_crossentropy',
              optimizer=Adam(1e-4),
              metrics=['accuracy'])

history = model.fit(train_dataset,
                    epochs=10,
                    validation_data=test_dataset,
                    validation_steps=30)
Exemple #9
0
SeqModelA.add(MaxPooling2D(pool_size=(2, 2)))
# add another convolutional layer
SeqModelA.add(Conv2D(30, (3, 3), activation='relu'))
SeqModelA.add(Conv2D(30, (3, 3), activation='relu'))
# pooling layer
SeqModelA.add(MaxPooling2D(pool_size=(2, 2)))
# Flatten the image to 1 dimensional array
SeqModelA.add(Flatten())
# add a dense layer : amount of nodes, activation
SeqModelA.add(Dense(500, activation='relu'))
# place a dropout layer
# 0.5 drop out rate is recommended, half input nodes will be dropped at each update
SeqModelA.add(Dropout(0.5))
# defining the ouput layer of our network
SeqModelA.add(Dense(43, activation='softmax'))
SeqModelA.compile(Adam(lr=0.001),
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])

SqeModelB = Sequential([
    Conv2D(16,
           3,
           padding='same',
           activation='relu',
           input_shape=(IMG_HEIGHT, IMG_WIDTH, 3)),
    MaxPooling2D(),
    Dropout(0.4),
    Conv2D(32, 3, padding='same', activation='relu'),
    MaxPooling2D(),
    Dropout(0.4),
    Conv2D(64, 3, padding='same', activation='relu'),
Exemple #10
0

# BEGIN MODEL
@tf.function
def forward(X):
    return tf.nn.softmax(tf.matmul(X, weights) + biases)


@tf.function
def accuracy(x, y):
    return 0


# END MODEL

optimizer = Adam()
batch_size = 64
epochs = 20
for epoch in range(0, epochs):
    train_iterator = tqdm(
        train_dataset.shuffle(buffer_size=len(traind)).batch(batch_size),
        desc=f'Training epoch {epoch}',
        total=math.ceil(len(traind) / batch_size))
    for images, labels in train_iterator:
        with tf.GradientTape(persistent=True) as tape:
            y_probabilities = forward(images)
            """predictions = []
            for probabilities in y_probabilities:
                prediction = tf.argmax(probabilities)
                predictions.append(prediction)
            predictions = np.array(predictions)"""
Exemple #11
0
from tensorflow_core.python.keras.optimizer_v2.adadelta import Adadelta
from tensorflow_core.python.keras.optimizer_v2.adagrad import Adagrad
from tensorflow_core.python.keras.optimizer_v2.adam import Adam
from tensorflow_core.python.keras.optimizer_v2.gradient_descent import SGD
from tensorflow_core.python.keras.optimizer_v2.rmsprop import RMSprop

GD = SGD(learning_rate=0.01)
ADA_GRAD = Adagrad(learning_rate=0.1)
RMS_PROP = RMSprop(learning_rate=0.1)
ADA_DELTA = Adadelta(learning_rate=1)
ADAM = Adam(learning_rate=0.1)
NESTEROV = SGD(learning_rate=0.01, momentum=0.5, nesterov=True)
Exemple #12
0
import tensorflow as tf

from tensorflow_core.python.keras.optimizer_v2.adam import Adam

# learn a linear shift between two arrays by gradient descent
# [1, 2, 1] -> [2, 3, 2] --> shift = 1

shift = tf.Variable(0, dtype=tf.float32)
optimizer = Adam(learning_rate=0.1)


@tf.function
def forward(X):
    return X + shift


y = tf.convert_to_tensor([2, 3.2, 2], dtype=tf.float32)
x = tf.convert_to_tensor([1, 2, 1], dtype=tf.float32)

for i in range(0, 1000):
    with tf.GradientTape() as tape:
        y_pred = forward(x)
        loss = tf.reduce_sum(tf.square(y - y_pred))

    gradient = tape.gradient(loss, shift)
    optimizer.apply_gradients([(gradient, shift)])

    print(f'Shift at step {i}: {shift.numpy()}')