1

i want to deal a regression problem using neural network, i use tensorflow to create the neural network, i hope the prediction of neural network to be more precise. but the accuracy of network would never exceed 0.5050 no matter how i modify the network, i tried to increase the node, using dropout and regularization, also the batch size and activation function, i don't know how to increase the accuracy, and i really need help, thanks.

here is my code

import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers


import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

nim = 201
njm = 201
rb = np.zeros((nim, 2))
rt = np.zeros((nim, 2))
rl = np.zeros((njm, 2))
rr = np.zeros((njm, 2))
rb_val = np.zeros(nim)
rt_val = np.ones(nim)
rl_val = np.zeros(njm)
rr_val = np.ones(njm)
rb_val_rev = np.zeros(nim)
rt_val_rev = np.zeros(nim)
rl_val_rev = np.zeros(njm)
rr_val_rev = np.zeros(njm)


for im in range(nim):
    xb = 4 * np.cos(im * np.pi * 1/(nim - 1))
    yb = 4 * np.sin(im * np.pi * 1/(nim - 1))
    rb[im] = np.array([xb, yb])
    rb_val_rev[im] = 1 - im * 1/(nim - 1)
    xt = 10 * np.cos(im * np.pi * 1/(nim - 1))
    yt = 10 * np.sin(im * np.pi * 1/(nim - 1))
    rt[im] = np.array([xt, yt])
    rt_val_rev[im] = 1 - im * 1/(nim - 1)
for jm in range(njm):
    xl = -4 - jm * 0.03
    yl = 0
    rl[jm] = np.array([xl, yl])
    rl_val_rev[jm] = jm * 1/(njm - 1)
    xr = 4 + jm * 0.03
    yr = 0
    rr[jm] = np.array([xr, yr])
    rr_val_rev[jm] = jm * 1/(njm - 1)


x_train = np.vstack((rl, rt, rr, rb))
y_train = np.hstack((rl_val, rt_val_rev, rr_val, rb_val_rev))
seed = 50
tf.random.set_seed(seed)
x_train_rand = tf.random.shuffle(x_train)
tf.random.set_seed(seed)
y_train_rand = tf.random.shuffle(y_train)
print(x_train)
print(y_train)


model = keras.Sequential([
    layers.Dense(100, activation=tf.nn.tanh, kernel_regularizer=tf.keras.regularizers.l2()),
    layers.Dropout(0.5),
    layers.Dense(100, activation=tf.nn.tanh, kernel_regularizer=tf.keras.regularizers.l2()),
    layers.Dropout(0.5),
    layers.Dense(1)
])

model.build(input_shape=(1, 2))
model.summary()


model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.01),
              loss='mse', metrics=['accuracy'])
# 模型训练
history = model.fit(x_train_rand, y_train_rand, batch_size=804, epochs=1000, shuffle=True,
                    validation_split=0.0, validation_freq=1)

plt.plot(history.history['accuracy'], label="train_ac", color='r', linewidth=2)
# plt.plot(history.history['val_accuracy'], label="test_ac", color='g', linewidth=2)
plt.plot(history.history['loss'], label="train_loss", color='y', linewidth=2)
# plt.plot(history.history['val_loss'], label="test_loss", color='b', linewidth=2)
plt.legend(loc='upper right')
plt.show()
model.save("hor_ks1.h5", save_format="")
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Jun 05 '22 at 15:09

1 Answers1

1

Accuracy makes no sense for regression problem. Accuracy means percentage of samlpes that you output perfectly correct answer, but you will never output perfect value in regression. Just change accuracy to MSE.

lejlot
  • 64,777
  • 8
  • 131
  • 164