美文网首页
线性回归|第一天的Keras

线性回归|第一天的Keras

作者: 静脉 | 来源:发表于2017-08-21 23:52 被阅读0次

    在这里假设大家都懂什么是机器学习,还有深度学习了.
    我们在这里训练一个regression模型,比较简单给5个点.找出方程y = w*x + b .

    废话不多说先贴代码

    from keras.models import Sequential
    from keras.layers import Dense
    import numpy as np
    
    
    x_train = [1, 2, 3, 4, 5]
    y_train = [2, 3, 4, 5, 6]
    x_train = np.array(x_train)
    y_train = np.array(y_train)
    # define a model
    model = Sequential()
    model.add(Dense(
        input_dim=1,
        units=1,
        use_bias=True,
    ))
    model.add(Dense(
        input_dim=1,
        units=1,
        use_bias=True,
    ))
    model.compile(loss='mse', optimizer='sgd')
    
    # training the model
    model.fit(x_train, y_train, batch_size=6, epochs=40, initial_epoch=0)
    
    # test the model
    score = model.evaluate(x_train, y_train, batch_size=5)
    test_data = model.predict(np.array([[5]]), batch_size=1)
    print test_data
    print model.layers[0].get_weights(), '\n', model.layers[1].get_weights()
    print score
    

    先来说第一段代码

    from keras.models import Sequential
    from keras.layers import Dense
    import numpy as np
    

    首先keras.models 下有一个Sequential型的model,用此model可建立一个线性的神经网络,什么是线性的?就是每个神经层头接尾,不形成回路。
    再说就是layers,字面意思是层,真实意思也是层-神经层.神经层也有好多种,在这里我们选择Dense层,因为此全链接层最常用.
    后面就是numpy,这个就不多说了,主要是处理数据用的.这里我们用它处理训练数据.

    好的.下一段代码

    x_train = [1, 2, 3, 4, 5]
    y_train = [2, 3, 4, 5, 6]
    x_train = np.array(x_train)
    y_train = np.array(y_train)
    

    这里要说的比较少,重要的是对训练数据的预处理.我们每个神经层能接受的数据格式都是 numpy array 所以在这里要进行一下转化.
    如果很好奇转化后是什么样的数据,可以print一下.

    NEXT

    model = Sequential()
    model.add(Dense(
        input_dim=1,
        units=1,
        use_bias=True,
    ))
    model.add(Dense(
        input_dim=1,
        units=1,
        use_bias=True,
    ))
    model.compile(loss='mse', optimizer='sgd')
    
    

    刚才说了sequentail,这里就是用sequential创建一个model.
    但是只 model = Sequential(),只是建立一个空壳,之间没有任何层.
    下面我们就用的dense为model建立层.
    首先看函数Dense()的参数:
    input_dim : 输入数据的维度,我们的训练数据可以看出来是一维的,所以input_dim=1
    units: 输出数据的维度,这里还是1,因为我们的训练数据都是一维的.
    use_bias: 就是要训练的函数(y = w*x + b)有没有b,当然要有了!
    那么,add()是什么鬼,add添加,就是添加神经层嘛.把dense层放进去,直接add就可以了.
    到这里我们的function就设好了.
    下面我们就要把模型编译好.
    直接compile()这里有两个参数:
    loss 是指 loss function. optimizer 是指优化器(说白了就是怎么找loss function 的最小值.里面有一些learnign rate等参数)
    因为里面有写好的类型'mse','sgd',所以直接用就好了.

    训练

    model.fit(x_train, y_train, batch_size=6, epochs=40, initial_epoch=0)
    

    把我们的训练数据放进去,指定每次训练多少个数据(batch_size=6),训练多少次(epochs=40)就OK了.

    看看效果

    # test the model
    score = model.evaluate(x_train, y_train, batch_size=5)
    test_data = model.predict(np.array([[5]]), batch_size=1)
    print test_data
    print model.layers[0].get_weights(), '\n', model.layers[1].get_weights()
    print score
    

    evaluate()评估一下模型的偏差.越低越好.
    那么我们是一下输入一个数,看看他能输出啥.predict()可以看到传入了一个5,得到的结果是6.023.这是我训练的结果.batch_size就是数据规模,只有一个当然是1了.
    好了,现在我们要看每层训练出来的w,b 就直接print model.layers[0].get_weights() 就好了

    Attention:给大家提个省,不要用训练数据作为测试数据.我是太懒了没有换数据.但实际操作过程中,训练数据要和测试数据分开哦.

    最后的输出结果

    /home/kroossun/miniconda2/bin/python "/home/kroossun/PycharmProjects/ML/test one.py"
    Using TensorFlow backend.
    Epoch 1/40
    2017-08-21 23:51:59.288807: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
    2017-08-21 23:51:59.288829: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
    2017-08-21 23:51:59.288835: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
    2017-08-21 23:51:59.288841: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
    2017-08-21 23:51:59.288845: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
    5/5 [==============================] - 0s - loss: 12.5863
    Epoch 2/40
    5/5 [==============================] - 0s - loss: 9.3619
    Epoch 3/40
    5/5 [==============================] - 0s - loss: 6.0762
    Epoch 4/40
    5/5 [==============================] - 0s - loss: 3.2606
    Epoch 5/40
    5/5 [==============================] - 0s - loss: 1.3951
    Epoch 6/40
    5/5 [==============================] - 0s - loss: 0.4658
    Epoch 7/40
    5/5 [==============================] - 0s - loss: 0.1648
    Epoch 8/40
    5/5 [==============================] - 0s - loss: 0.0841
    Epoch 9/40
    5/5 [==============================] - 0s - loss: 0.0653
    Epoch 10/40
    5/5 [==============================] - 0s - loss: 0.0609
    Epoch 11/40
    5/5 [==============================] - 0s - loss: 0.0594
    Epoch 12/40
    5/5 [==============================] - 0s - loss: 0.0585
    Epoch 13/40
    5/5 [==============================] - 0s - loss: 0.0576
    Epoch 14/40
    5/5 [==============================] - 0s - loss: 0.0569
    Epoch 15/40
    5/5 [==============================] - 0s - loss: 0.0561
    Epoch 16/40
    5/5 [==============================] - 0s - loss: 0.0553
    Epoch 17/40
    5/5 [==============================] - 0s - loss: 0.0546
    Epoch 18/40
    5/5 [==============================] - 0s - loss: 0.0538
    Epoch 19/40
    5/5 [==============================] - 0s - loss: 0.0531
    Epoch 20/40
    5/5 [==============================] - 0s - loss: 0.0524
    Epoch 21/40
    5/5 [==============================] - 0s - loss: 0.0517
    Epoch 22/40
    5/5 [==============================] - 0s - loss: 0.0510
    Epoch 23/40
    5/5 [==============================] - 0s - loss: 0.0503
    Epoch 24/40
    5/5 [==============================] - 0s - loss: 0.0497
    Epoch 25/40
    5/5 [==============================] - 0s - loss: 0.0490
    Epoch 26/40
    5/5 [==============================] - 0s - loss: 0.0483
    Epoch 27/40
    5/5 [==============================] - 0s - loss: 0.0477
    Epoch 28/40
    5/5 [==============================] - 0s - loss: 0.0470
    Epoch 29/40
    5/5 [==============================] - 0s - loss: 0.0464
    Epoch 30/40
    5/5 [==============================] - 0s - loss: 0.0458
    Epoch 31/40
    5/5 [==============================] - 0s - loss: 0.0452
    Epoch 32/40
    5/5 [==============================] - 0s - loss: 0.0446
    Epoch 33/40
    5/5 [==============================] - 0s - loss: 0.0440
    Epoch 34/40
    5/5 [==============================] - 0s - loss: 0.0434
    Epoch 35/40
    5/5 [==============================] - 0s - loss: 0.0428
    Epoch 36/40
    5/5 [==============================] - 0s - loss: 0.0423
    Epoch 37/40
    5/5 [==============================] - 0s - loss: 0.0417
    Epoch 38/40
    5/5 [==============================] - 0s - loss: 0.0411
    Epoch 39/40
    5/5 [==============================] - 0s - loss: 0.0406
    Epoch 40/40
    5/5 [==============================] - 0s - loss: 0.0401
    5/5 [==============================] - 0s
    [[ 6.18166256]]
    [array([[-1.10607874]], dtype=float32), array([-0.21471027], dtype=float32)] 
    [array([[-1.02117157]], dtype=float32), array([ 0.31492552], dtype=float32)]
    0.0395184643567
    
    Process finished with exit code 0
    
    

    相关文章

      网友评论

          本文标题:线性回归|第一天的Keras

          本文链接:https://www.haomeiwen.com/subject/wjtkdxtx.html