数学基础

  • 张量tensor:数字容器。矩阵是二维张量
  • 皱axis:张量的维度
  • 标量scalar:仅包含一个数字的张量,0D张量,0个轴

    1. import numpy as np
    2. x = np.array(12)
    3. x.ndim
  • 向量:1D张量数字组成的数组,注意5D向量和5D张量是两种

  • 举证:2D张量
  • 张量属性

    • 轴的个数:阶,即ndim
    • 形状:每个轴维度大小
    • 数据类型

      keras

  • 标准化

    1. mean = train_data.mean(axis = 0)
    2. train_data -= mean
    3. std = train_data.std(axis = 0)
    4. train /= std
  • one_hot

    1. def vectorize_sequence(sequences,dimension = 10000):
    2. results = np.zeros((len(sequences),dimension))
    3. for i,sequence in enumerate(sequences):
    4. results[i,sequence] = 1
    5. return results
  • 向量化 ```python y_train = np.asarray(train_labels).astype(‘float32’)

or

from keras.utils.np_utils import to_categorical one_hot_train_labels = to_categorical(train_labels)

  1. - 建立模型
  2. ```python
  3. from keras import models
  4. from keras import layers
  5. model = models.Sequential()
  6. model.add(layers.Dense(16,activation = 'relu',input_shape = (10000,)))
  7. model.add(layers.Dense(16,activation = 'relu'))
  8. model.add(layers.Dense(16,activation = 'sigmoid'))
  9. model.compile(
  10. optimizer = 'rmsprop',
  11. loss = 'binary_crossentropy',
  12. metrics = ['acc']
  13. )
  • 训练

    1. model.fit(train_data,train_labels,epochs = 20,batch_size = 512,validation_data = (x_val,y_val))
  • 画图:选择合适的epoch

    1. import matplotlib.pyplot as plot
    2. loss_values = history.history['loss']
    3. val_loss_values = history.history['val_loss']
    4. plt.plot(epochs,loss_values,'bo',label = '')
    5. plt.plot(epochs,val_loss_values,'b',label = '')
    6. plt.legend()
    7. plt.show()
  • 评估

    1. model.evaluate(test_data,test_label)
  • 预测

    1. model.predict(x_test)

    问题分类

  • 二分类

  • 多分类
  • 回归