在往期的神经网络中,我们训练样本的时候需要成千上万的样本数据,在对这些数据进行收集和打标签的时候,往往需要付出比较多的代价。比如我们需要采集某个型号的设备开启时一段时间内的信号,那么我们需要对该种型号的设备,开启成千上万次,才能采集到那么多电信号用来训练,这无疑对我们的设备造成损害。因此,使用更少的样本学习到更多的特征,成为机器学习所追求的目标之一。
常说的one-shot learning和few-shot learning,都是指的是通过一个及少量的样本习得模型,然后具有分类的能力。
“one shot learning aims to learn information about object categories from one, or only a few, training samples/images” --wiki
“few-shot learning is refers to the practice of feeding a learning model with a very small amount of training data, contrary to the normal practice of using a large amount of data.” --Dr.Michael J.Garbade
所以说 one-shot learning 和 few shot learning 中的one 和few指的是在学习分类过程中,training samples 的数量。比如我们上一节讲的电信号分为九类,one-shot,那么就是有9个样本,每个类别一个,few-shot 就是每个类别多于一个样本,到极端情况下,我们会碰都某些类别中有0个训练样本,那么这种极端情况叫做zero-shot learning。
像这种小样本学习,他们的判别方法是通过判断进来的类型是否为同一类来作为分类的方法,在测试的时候,通过将待测数据通过习得的模型,来进行分类。
所以在训练的时候,我们也需要同时输入进去不同的样本个体,而对应的y值,分别用0和1来表示,如果输入模型中的样本为同一类那么y值为0如果不是同一类那么y值为1。通过这样的方式,我们可以减少样本训练时所需要的样本量。
而siamese neural network (孪生神经网络)使用了两个之前讲过的卷积神经网络,来作为两个样本的输入,从而搭建出小样本学习框架。
“A siamese neural network (sometimes called a twin neural network) is an artificial neural network that uses the same weights while working in tandem on two different input vectors to compute comparable output vectors." --wiki
可以简单理解为下图:
所以原理其实很简单,那么下面我们还是使用上一篇文章使用的数据集,来进行siamese network编码。
一、导入包和数据
import os
import csv
import pandas as pd
import keras
import numpy as np
from keras.models import Sequential,Model
from keras.layers import Dense, Activation, Flatten, Convolution1D, Dropout, MaxPooling1D, Inputimport time
from datetime import datetime
from keras import backend as K
from keras.layers.core import Lambda, Flatten, Dense
path=r'E:\ilm\train_data'
files=os.listdir(path)
二、处理数据使之变为两个vectors(向量),并设好y值。
column_names=[]
train=pd.DataFrame()
for item in files:
data_frame=pd.read_csv('E:/ilm/train_data/'+item)
data_frame['mins']=range(len(data_frame))
data_frame=data_frame.drop(['Unnamed: 0'],axis=1)
data_frame=pd.pivot_table(data_frame,columns=['mins'])
train=train.append(data_frame)
train=train[:140]
train.reset_index()
train1=train.sort_index()
train2=train1.groupby(train1.index).head(9)
train2=train2.groupby