我应该如何提高模型的准确性

时间:2019-06-26 21:29:47

标签: machine-learning keras neural-network deep-learning conv-neural-network

我生成图像数据的np数组,然后将其用于训练模型,但是模型的精度低于10%,这意味着我的模型没有学习。我只是想知道是否有人可以看一下我用来生成图像数据的代码和我用来训练模型的代码,并帮助我改进代码以使准确性达到80%到90%。

我尝试更改优化器和损失函数,但我所做的任何更改都无法对其进行改进。

'''此代码使用函数生成图像,然后将输出数据另存为PC上的np阵列'''                 #--编码:utf-8--                 “”                 创建于2019年6月20日星期四14:41:03

            @author: franc
            """
            import numpy as np
            import matplotlib.pyplot as plt
            import os
            import math
            #import cv2
            A = 1
            xctr= 16
            yctr= 16

            y=(np.repeat(np.arange(0,33,1),33,axis=0)).reshape(33,33)
            x=np.transpose(y)

            datalist=[]
            testlist=[]
            infolist=[]
            wavelengthList = []
            thetaList = []
            phiList = []
            ampList = []
            for lamb in np.arange (8,24,1.0):
                for theta in np.arange(0.0, 3.14159,.167):
                    kx=float((6.28318/lamb)*(math.cos(theta)))
                    ky=float((6.28318/lamb)*(math.sin(theta)))
                    for phi in np.arange(0.0,6.28318,.333):
                        datalist.append(A * np.cos((kx * (x-xctr))+(ky * (y-yctr)) + phi))
                        wavelengthList.append(lamb)
                        thetaList.append(theta)
                        phiList.append(phi)


np.save(os.path.join('C:\\Users\\franc\\Desktop\\Tdata','training data'), datalist)

np.save(os.path.join('C:\\Users\\franc\\Desktop\\Tlabel','training wavelengthList'), wavelengthList)

np.save(os.path.join('C:\\Users\\franc\\Desktop\\Tlabel','training thetaList'),thetaList )

np.save(os.path.join('C:\\Users\\franc\\Desktop\\Tlabel','training phiList'), phiList)
            plt.imshow(datalist[1], cmap='gray')
            plt.show()
            plt.imshow(datalist[200], cmap='gray')

            plt.show()

'''此代码训练模型'''

    import tensorflow as tf
    import numpy as np
    from tensorflow.keras.callbacks import TensorBoard 
    import time

    """Build data and label arrays to feed neural network"""
    data = np.array(np.load('C:\\Users\\franc\\Desktop\\Tdata\\training data.npy'))
    wavelengthArray = np.array(np.load('C:\\Users\\franc\\Desktop\\Tlabel\\training wavelengthList.npy'))
    thetaArray= np.array(np.load('C:\\Users\\franc\\Desktop\\Tlabel\\training thetaList.npy'))
    phiArray= np.array(np.load('C:\\Users\\franc\\Desktop\\Tlabel\\training phiList.npy'))

    NAME = "Trained-Model-{}".format(int(time.time()))


    #Testing data
    #testData=np.array(np.load('C:\\Users\\franc\\Desktop\\testData\\testing data.npy'))

    """Train neural network"""
    inputLayer = tf.keras.layers.Input(shape=(33,33))
    flatInput = tf.keras.layers.Flatten()(inputLayer)




    x = tf.keras.layers.Dense(64, activation='relu')(flatInput)
    x = tf.keras.layers.Dense(64, activation='relu')(x)
    x = tf.keras.layers.Dense(64, activation='relu')(x)
    x = tf.keras.layers.Dense(64, activation='relu')(x)

    wavelengthLayer = tf.keras.layers.Dense(1, activation='linear')(x)
    thetaLayer = tf.keras.layers.Dense(1, activation='linear')(x)
    phiLayer = tf.keras.layers.Dense(1, activation='linear')(x)
    #ampLayer = tf.keras.layers.Dense(1, activation='linear')(x)

    model = tf.keras.models.Model(inputs=inputLayer, outputs=[wavelengthLayer,
            thetaLayer, phiLayer])

    tensorboard = TensorBoard(log_dir="logs/{}".format(NAME))

    model.compile(optimizer='rmsprop', loss='mse',metrics=['accuracy'])
    model.fit(data, [wavelengthArray, thetaArray, phiArray],epochs=100,batch_size=10, shuffle=True, callbacks=[tensorboard])
    model.save("Wave Model.model")

    '''score = model.evaluate(data, [wavelengthArray, thetaArray, phiArray], verbose=2)
    print('Test loss:', score[0])
    print('Test accuracy:', score[1])
    print('Test accur:', score[2])
    print('Test accur:', score[3])'''

史诗1/100 5776/5776 [==============================]-3s 555us / sample-损耗:8.9422-density_4_loss:6.5498-density_5_loss :0.5684-density_6_acc:0.00203e-00-density_5_acc:0.0097-density_6_acc:0.0016
时代2/100 5776/5776 [==============================]-2s 382us / sample-损耗:2.4699-density_4_loss:1.4102-density_5_loss :0.1807-density_6_acc:0.00795-density_4_acc:0.0000e + 00-density_5_acc:0.0232-density_6_acc:0.0024 时代3/100 5776/5776 [==============================]-2s 332us / sample-损耗:1.8800-density_4_loss:1.0962-density_5_loss :0.1332-density_6_acc:0.00500e + 00-density_5_acc:0.0296-density_6_acc:0.0064 时代4/100 5776/5776 [===============================]-2s 352us / sample-损耗:1.4607-density_4_loss:0.8816-density_5_loss :0.1081-density_6_acc:0.0121-density_4_acc:0.0000e + 00-density_5_acc:0.0364-density_6_acc:0.0151 时代5/100 5776/5776 [===============================]-2s 329us / sample-损耗:1.2339-density_4_loss:0.7418-density_5_loss :0.0964-density_6_acc:0.0959-density_6_acc:0.0223-density_4_acc:0.0000e + 00-density_5_acc:0.0369- 时代6/100 5776/5776 [===============================]-2s 332us / sample-损耗:1.0370-density_4_loss:0.6581-density_5_loss :0.0794-density_6_acc:0.0990-density_4_acc:0.0000e + 00-density_5_acc:0.0431-density_6_acc:0.0272 时代7/100 5776/5776 [==============================]-2s 328us / sample-损耗:0.9456-density_4_loss:0.6048-density_5_loss :0.0713-density_6_acc:0.0692-density_4_acc:0.0000e + 00-density_5_acc:0.0436-density_6_acc:0.0312 时代8/100 5776/5776 [==============================]-2s 417us / sample-损耗:0.8453-density_4_loss:0.5664-density_5_loss :0.0611-density_6_acc:0.0237-density_6_acc:0.0370-density_4_acc:0.0000e + 00-density_5_acc:0.0462- 时代9/100 5776/5776 [===============================]-2s 415us / sample-损耗:0.7624-density_4_loss:0.5120-density_5_loss :0.0602-density_6_acc:0.0900-density_6_acc:0.0358-density_6_acc:0.0000e + 00-density_5_acc:0.0469- 时代10/100 5776/5776 [===============================]-2s 338us / sample-损耗:0.6950-density_4_loss:0.4702-density_5_loss :0.0510-density_6_acc:0.0741-density_6_acc:0.0414-density_4_acc:0.0000e + 00-density_5_acc:0.0495 时代11/100 5776/5776 [===============================]-2s 311us / sample-损耗:0.6295-density_4_loss:0.4350-density_5_loss :0.0486-density_6_acc:0.0458-density_6_acc:0.0409-density_4_acc:0.0000e + 00-density_5_acc:0.0483- 时代12/100 5776/5776 [===============================]-2s 321us / sample-损耗:0.5678-density_4_loss:0.3932-density_5_loss :0.0441-density_6_acc:0.0304-density_4_acc:0.0000e + 00-density_5_acc:0.0488-density_6_acc:0.0433 时代13/100 5776/5776 [==============================]-2s 330us / sample-损耗:0.5265-density_4_loss:0.3695-density_5_loss :0.0404-density_6_acc:0.0165-density_4_acc:0.0000e + 00-density_5_acc:0.0504-density_6_acc:0.0433 时代14/100 5776/5776 [===============================]-2s 324us / sample-损耗:0.5042-density_4_loss:0.3581-density_5_loss :0.0397-density_6_acc:0.0064-density_4_acc:0.0000e + 00-density_5_acc:0.0509-density_6_acc:0.0452 时代15/100 5776/5776 [==============================]-2s 327us / sample-损耗:0.4659-density_4_loss:0.3311-density_5_loss :0.0345-density_6_acc:0.0003-density_4_acc:0.0000e + 00-density_5_acc:0.0509-density_6_acc:0.0452 时代16/100 5776/5776 [==============================]-2s 343us / sample-损耗:0.4402-density_4_loss:0.3126-density_5_loss :0.0356-density_6_acc:0.0918-density_4_acc:0.0000e + 00-density_5_acc:0.0500-density_6_acc:0.0461 时代17/100 5776/5776 [===============================]-2s 331us / sample-损耗:0.4245-density_4_loss:0.2960-density_5_loss :0.0342-density_6_acc:0.0941-density_4_acc:0.0000e + 00-density_5_acc:0.0512-density_6_acc:0.0476 时代18/100 5776/5776 [==============================]-2s 334us / sample-损耗:0.3903-density_4_loss:0.2805-density_5_loss :0.0319-density_6_acc:0.0780-density_4_acc:0.0000e + 00-density_5_acc:0.0512-density_6_acc:0.0481 时代19/100 5776/5776 [===============================]-2s 331us / sample-损失:0.3696-density_4_loss:0.2774-density_5_loss :0.0310-density_6_acc:0.0613-density_4_acc:0.0000e + 00-density_5_acc:0.0516-density_6_acc:0.0497 时代20/100 5776/5776 [===============================]-2s 425us / sample-损耗:0.3568-density_4_loss:0.2631-density_5_loss :0.0281-density_6_acc:0.0654-density_4_acc:0.0000e + 00-density_5_acc:0.0516-density_6_acc:0.0488 时代21/100 5776/5776 [==============================]-2s 357us / sample-损耗:0.3422-density_4_loss:0.2448-density_5_loss :0.0264-density_6_acc:0.0709-density_4_acc:0.0000e + 00-density_5_acc:0.0516-density_6_acc:0.0478

0 个答案:

没有答案