Esempio n. 1
0
    def __init__(self):
        self.grid = Grid()

        self.wind1 = Momentum(flux_choice="center", TI ="AB")
        self.wind2 = Momentum(flux_choice="JS", TI="AB")
        self.wind3 = Momentum(flux_choice="center", TI="RK3")
        self.wind4 = Momentum(flux_choice="JS", TI="RK3")
Esempio n. 2
0
    def fromfccptc(cls, fccptc):
        """
			Classmethod that creates Particle from FCC EDM MCParticle

			Args:
			fccptc [MCParticle]: the MCParticle to use
		"""

        pdgid = fccptc.Core().Type
        mass = fccptc.Core().P4.Mass
        p = Momentum(fccptc.Core().P4.Px,
                     fccptc.Core().P4.Py,
                     fccptc.Core().P4.Pz)
        start_vertex = Vertex(fccptc.StartVertex().Position().X,
                              fccptc.StartVertex().Position().Y,
                              fccptc.StartVertex().Position().Z
                              ) if fccptc.StartVertex().isAvailable() else None
        end_vertex = Vertex(fccptc.EndVertex().Position().X,
                            fccptc.EndVertex().Position().Y,
                            fccptc.EndVertex().Position().Z
                            ) if fccptc.EndVertex().isAvailable() else None
        status = fccptc.Core().Status
        charge = fccptc.Core().Charge

        return cls(pdgid, mass, p, start_vertex, end_vertex, status, charge)
Esempio n. 3
0
def dataLearning(x_data, y_data, learning_rate, momentum, step, a, b):
    num_points = len(x_data)

    # 그래프 출력에 사용할 x 최소값, 최대값
    min_x = min(x_data)
    max_x = max(x_data)

    # momentum optimizer
    momentumOptimizer = Momentum(lr=learning_rate, momentum=momentum)
    optimizerParams = {"a": a, "b": b}
    optimizerGrads = {"a": 0, "b": 0}

    all_loss = []
    all_step = []
    last_a = a
    last_b = b

    for step in range(1, step + 1):
        loss = 0
        all_da = 0
        all_db = 0

        y_star = optimizerParams["a"] * x_data + optimizerParams["b"]
        loss = linear_loss(optimizerParams["a"], optimizerParams["b"], x_data,
                           y_data)

        optimizerGrads["a"] = da(y_data, y_star, x_data)
        optimizerGrads["b"] = db(y_data, y_star)

        all_loss.append(loss)
        all_step.append(step)

        last_a = optimizerParams["a"]
        last_b = optimizerParams["b"]

        momentumOptimizer.update(params=optimizerParams, grads=optimizerGrads)

    return [last_a, last_b]
Esempio n. 4
0
model = densenet.DenseNet(img_dim,
                          classes=nb_classes,
                          depth=depth,
                          nb_dense_block=nb_dense_block,
                          growth_rate=growth_rate,
                          nb_filter=nb_filter,
                          dropout_rate=dropout_rate,
                          bottleneck=bottleneck,
                          reduction=reduction,
                          weights=None)
func = K.function([model.layers[0].input], [model.layers[-2].output])

print("Model created")

optimizer = Momentum(lr=1e-3, decay=1e-4)
model.compile(loss='categorical_crossentropy',
              optimizer=optimizer,
              metrics=["accuracy"])

(trainX, trainY), (testX, testY) = cifar10.load_data()

trainX = trainX.astype('float32') / 255.
testX = testX.astype('float32') / 255.

trainX_mean = np.mean(trainX, axis=0)
trainX -= trainX_mean
testX -= trainX_mean

Y_train = np_utils.to_categorical(trainY, nb_classes)
Y_test = np_utils.to_categorical(testY, nb_classes)
Esempio n. 5
0
dropout_rate = 0.0  # 0.0 for data augmentation

model = densenet.DenseNet(img_dim,
                          classes=nb_classes,
                          depth=depth,
                          nb_dense_block=nb_dense_block,
                          growth_rate=growth_rate,
                          nb_filter=nb_filter,
                          dropout_rate=dropout_rate,
                          bottleneck=bottleneck,
                          reduction=reduction,
                          weights=None)

print("Model created")

optimizer = Momentum(decay=1e-4)
model.compile(loss='categorical_crossentropy',
              optimizer=optimizer,
              metrics=["accuracy"])

(trainX, trainY), (testX, testY) = cifar10.load_data()

trainX = trainX.astype('float32') / 255.
testX = testX.astype('float32') / 255.

trainX_mean = np.mean(trainX, axis=0)
trainX -= trainX_mean
testX -= trainX_mean

Y_train = np_utils.to_categorical(trainY, nb_classes)
Y_test = np_utils.to_categorical(testY, nb_classes)
Esempio n. 6
0
                    kernel_initializer='he_normal')(y)

    # Instantiate model.
    model = Model(inputs=inputs, outputs=outputs)
    return model


"""## Create model"""

if version == 2:
    model = resnet_v2(input_shape=input_shape, depth=depth)
else:
    model = resnet_v1(input_shape=input_shape, depth=depth)
"""## Model compilation"""
model.compile(loss='categorical_crossentropy',
              optimizer=Momentum(1e-3, beta=0.9, decay=1e-3),
              metrics=['accuracy'])

save_dir = '.'

model_name = 'epoch-{epoch:03d}.h5'

filepath = os.path.join(save_dir, model_name)

# Prepare callbacks for model saving and for learning rate adjustment.
checkpoint = ModelCheckpoint(filepath=filepath,
                             monitor='val_acc',
                             verbose=1,
                             save_best_only=True,
                             period=1)
Esempio n. 7
0
    # 시작점
    a = -20.0
    b = -20.0

    # 점의 개수
    num_points = 50
    # 데이터 생성
    data = twoDData(num_points, 5, 5, 10, 5)
    x, y = data.dataGeneration()

    # 그래프 출력에 사용할 x 최소값, 최대값
    min_x = min(x)
    max_x = max(x)

    # momentum optimizer
    momentumOptimizer = Momentum(lr=rate, momentum=momentum)
    optimizerParams = {"a": a, "b": b}
    optimizerGrads = {"a": 0, "b": 0}

    # ----- 1,1 subplot 부분 ------

    # 평면 출력
    [aa, bb, MSE] = show_surface(x, y)
    # 전치 행렬로 바꾸기
    SSE = MSE.T

    fig = plt.figure(1, figsize=(12, 8))
    fig.suptitle('momentum SGD learning rate: %.4f' % (rate), fontsize=20)

    ax = fig.add_subplot(2, 2, 1, projection='3d')
    ax.plot_surface(aa, bb, MSE, rstride=2, cstride=2, cmap='rainbow')
Esempio n. 8
0
        return train(x_train,t_train,x_test,t_test,network,optimizer,iters_num)
    return train_NN

import matplotlib.pyplot as plt

(x_train, t_train), (x_test, t_test) = load_mnist(normalize=True, one_hot_label=True)

training = Prepare_training(x_train,t_train,x_test,t_test)

from SGD import SGD
from Momentum import Momentum
from AdaGrad import AdaGrad
from Adam import Adam

optimizer1 = SGD(0.01)
optimizer2 = Momentum(0.01,0.9)
optimizer3 = AdaGrad(0.01)
optimizer4 = Adam(0.01)

train_loss_list1,train_acc_list1,test_acc_list1 = training(optimizer1,[50,50,50],200,0.01)
train_loss_list2,train_acc_list2,test_acc_list2 = training(optimizer2,[50,50,50],200,0.01)
train_loss_list3,train_acc_list3,test_acc_list3 = training(optimizer4,[50,50,50],200,0.01)

ones = np.ones(5)/5.0

plt.subplot(1,2,1)
plt.plot(np.convolve(train_loss_list1,ones,'valid'),label="SGD")
plt.plot(np.convolve(train_loss_list2,ones,'valid'),label="Momentum")
plt.plot(np.convolve(train_loss_list3,ones,'valid'),label="Adam")

plt.subplot(1,2,2)
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from Rosenbrock import  Rosenbrock
from SGD import SGD
from Momentum import Momentum

plt.rc('font', family='Malgun Gothic')
plt.rc('axes', unicode_minus=False)

momentum = 0.9
learning_rate = 0.000015
step = 5000

x, y = -5,-5 # starting_point

Momentum_model = Momentum(lr = learning_rate, momentum = momentum)
# Momentum_model = SGD(lr = learning_rate)
Rosenbrock_model = Rosenbrock()

xx = np.linspace(-5, 5, 100)
yy = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(xx, yy)
Z = Rosenbrock_model.f2(X, Y)


fig = plt.figure(1, figsize=(12, 8))
fig.suptitle('momentum SGD', fontsize=15)
ax = fig.add_subplot(2, 2, 1, projection='3d')
ax.set_top_view()
ax.plot_surface(X, Y, Z, rstride=2, cstride=2, cmap='rainbow')
Esempio n. 10
0
class Simulation:

    def __init__(self):
        self.grid = Grid()

        self.wind1 = Momentum(flux_choice="center", TI ="AB")
        self.wind2 = Momentum(flux_choice="JS", TI="AB")
        self.wind3 = Momentum(flux_choice="center", TI="RK3")
        self.wind4 = Momentum(flux_choice="JS", TI="RK3")

    def initialize(self):
        self.wind1.initialize(self.grid)
        self.wind2.initialize(self.grid)
        self.wind3.initialize(self.grid)
        self.wind4.initialize(self.grid)



    def run(self, ntimestep, plot=False):
        self.wind1.initial_stepAB()

        self.wind2.initial_stepAB()


        for _ in range(2):
            self.wind3.update()
            self.wind4.update()

        for _ in range(ntimestep):

            self.wind1.update()

            self.wind2.update()

            self.wind3.update()

            self.wind4.update()

        if plot == True:
            fig = plt.figure(3)
            plt.plot(self.grid.x, self.wind1.u)
            plt.show()

    def plot_energy(self):
        Efig = plt.figure(2)
        Eax = plt.axes()

        plt.xlabel(r"$t$", size=19)
        plt.ylabel(r"$Energy\quad(\;\frac{1}{2}\;u^2\;)$", size=19)

        Eax.grid()

        line1, = plt.plot(self.wind1.timesteps, self.wind1.energy, "b", label="AB center")
        line2, = plt.plot(self.wind2.timesteps, self.wind2.energy, "g", label="AB WENO")
        line3, = plt.plot(self.wind4.timesteps, self.wind4.energy, "y", label="RK WENO")
        line4, = plt.plot(self.wind3.timesteps, self.wind3.energy, "r", label="RK center")

        first_legend = plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3,
                   ncol=2, mode="expand", borderaxespad=0.)

        plt.show()

    def plot_movie(self):
        fig = plt.figure(1)
        ax  = plt.axes(xlim=(0, (self.grid.nx + 5)), ylim=((np.amin(self.wind1.u)-.5), (np.amax(self.wind1.u)+.5)))
        ax.grid()

        line, = ax.plot([], [])

        time_text   = ax.text(8, (np.amax(self.wind1.u) + .1), '', size=15)
        energy_text = ax.text(8, (np.amax(self.wind1.u)), '', size=15)

        # called between frames to clear the figure
        def init():
            line.set_data([], [])
            time_text.set_text('')
            energy_text.set_text('')
            return line, time_text, energy_text

        self.wind1.initial_stepAB()

        # animation function.  This is called sequentially
        def animate(i):
            self.wind1.update()
            line.set_data(self.grid.x, self.wind1.u)
            time_text.set_text('time = %.1f' % self.wind1.time_elapsed)
            energy_text.set_text('energy = %.3f J' % self.wind1.Energy())
            return line, time_text, energy_text

        anim = animation.FuncAnimation(fig, animate, init_func=init,
                                       frames=10, interval=7, blit=True)

        plt.show()