数学基础
- 张量tensor:数字容器。矩阵是二维张量
- 皱axis:张量的维度
标量scalar:仅包含一个数字的张量,0D张量,0个轴
import numpy as np
x = np.array(12)
x.ndim
向量:1D张量数字组成的数组,注意5D向量和5D张量是两种
- 举证:2D张量
张量属性
标准化
mean = train_data.mean(axis = 0)
train_data -= mean
std = train_data.std(axis = 0)
train /= std
one_hot
def vectorize_sequence(sequences,dimension = 10000):
results = np.zeros((len(sequences),dimension))
for i,sequence in enumerate(sequences):
results[i,sequence] = 1
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)
- 建立模型
```python
from keras import models
from keras import layers
model = models.Sequential()
model.add(layers.Dense(16,activation = 'relu',input_shape = (10000,)))
model.add(layers.Dense(16,activation = 'relu'))
model.add(layers.Dense(16,activation = 'sigmoid'))
model.compile(
optimizer = 'rmsprop',
loss = 'binary_crossentropy',
metrics = ['acc']
)
训练
model.fit(train_data,train_labels,epochs = 20,batch_size = 512,validation_data = (x_val,y_val))
画图:选择合适的epoch
import matplotlib.pyplot as plot
loss_values = history.history['loss']
val_loss_values = history.history['val_loss']
plt.plot(epochs,loss_values,'bo',label = '')
plt.plot(epochs,val_loss_values,'b',label = '')
plt.legend()
plt.show()
评估
model.evaluate(test_data,test_label)
预测
model.predict(x_test)
问题分类
二分类
- 多分类
- 回归