CNN与keras一起预测能量

时间:2019-08-29 07:47:39

标签: python keras regression

我是机器学习和keras库的新手,我制作了如下的CNN代码进行回归。

%matplotlib inline
from __future__ import division
import numpy as np
from numpy.random import rand
import matplotlib.pyplot as plt


def initial_spin_state(N):
    state = np.random.choice((0.11111, 0.99999), (N, N))
    return state


def metropolis_algorithm(config, beta):

    N = len(config)

    for i in range(N):
        for j in range(N):

            a = np.random.randint(0, N)
            b = np.random.randint(0, N)
            s = config[a, b]
            near=config[(a+1)%N,b] + config[a,(b+1)%N] + config[(a-1)%N,b] + config[a,(b-1)%N]

            delta = 2 * s *near
            if delta < 0:
                s *= -1
            elif rand() < np.exp(-delta * beta):
                s *= -1
            config[a, b] = s

    return config

def get_energy(config):
    energy = 0
    N = len(config)
    for i in range(N):
        for j in range(N):
            S = config[i, j]
            near = config[(i+1)%N, j] + config[i,(j+1)%N] + config[(i-1)%N, j] + config[i,(j-1)%N]
            energy += near*S

    return energy


x_train = []
y_train = []
for i in range(50000):

    config = initial_spin_state(16)
    energy = get_energy(config)

    x_train.append(config)
    y_train.append(energy)

x_train = np.array(x_train)
y_train = np.array(y_train)

print(x_train.shape)
print(y_train.shape)


x_test = []
y_test = []
for j in range(20000):

    config = initial_spin_state(16)
    energy = get_energy(config)

    x_test.append(config)
    y_test.append(energy)

x_test = np.array(x_test)
y_test = np.array(y_test)

print(x_test.shape)
print(y_test.shape)


x_train = x_train.reshape(50000, 16, 16, 1)
x_test = x_test.reshape(20000, 16, 16, 1)
print(x_train.shape)
print(x_test.shape)

import numpy as np
import tensorflow as tf
tf.logging.set_verbosity(tf.logging.ERROR)
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout
from keras.optimizers import Adadelta
from keras.regularizers import l2


model = Sequential()
model.add(Conv2D(32, (2, 2), input_shape = (16, 16, 1), activation = 'relu'))
model.add(MaxPooling2D(2,2))


model.add(Conv2D(16, (2, 2), activation = 'relu'))
model.add(MaxPooling2D(2,2))

model.add(Flatten())
model.add(Dense(512, activation = 'relu'))
#model.add(Dense(1024, activation = 'relu'))
model.add(Dense(1))

np.random.seed(0)
model.summary()
model.compile(loss = "mse", metrics = ['accuracy'], optimizer = 'adam')



%%time
hist = model.fit(x_train, y_train, epochs = 200, batch_size = 500,
                validation_data = (x_test, y_test), verbose = 2)


import matplotlib.pyplot as plt
plt.plot(hist.history['acc'], '_b', label = "training")
plt.plot(hist.history['val_acc'], 'r:', label = "test")
plt.legend()
plt.grid("on")
plt.show()


此代码用于图像输入和连续的能量值输出。

因此,如果我将图像(正在配置的图像)放置到CNN上,它应该可以预测该配置的能量。

问题是..

当我训练CNN时,训练损失和验证损失的降低非常缓慢。

当然,训练准确性和验证准确性的增长非常缓慢。

,有时只会提高训练的准确性,而不会提高val准确性。

好心的家伙..我的代码怎么了?

请教我

1 个答案:

答案 0 :(得分:0)

首先,由于您正在做回归问题,所以我认为使用acc作为度量标准不是一个好主意,相反,您可以考虑使用平均绝对误差mae作为矩阵。

您正在使用的损失为mse(平均平方错误),因此该值将非常大,尤其是当您不对y值进行归一化时。但是,在运行了大约25个提供的代码之后,validation loss降为{{1},其中290.xx13.xx(并且收敛然而)。我尝试使用您的模型来预测一些验证数据,但效果很好。也许您应该在假设出现问题之前测试模型。