Ejemplo n.º 1
0
from feature_maker import FeatureMaker
from nn import NeuralNetwork
from regularizor import L2Regularization
from learning_rate import MomentumLearningRate

src = ROOT_DIR + '/../../Data/iris/iris_data.csv'
feature_types = [1, 1, 1, 1, 0]
feature_maker = FeatureMaker(src=src,
                             delimiter=',',
                             types=feature_types,
                             norm=True)
X, Y = feature_maker.make(skip_rows=1, skip_cols=1)
# print("X : {}".format(X))
# print("Y : {}".format(Y))

train_X, test_X, train_Y, test_Y = feature_maker.train_test_split(
    X, Y, test_size=0.2, random_state=1234)
# train_X, test_X, train_Y, test_Y = train_X[:5,:], test_X[:5,:], train_Y[:5,:], test_Y[:5,:]
print("Train X : {}\n...".format(train_X[:5, :]))
print("Train Y : {}\n...".format(train_Y[:5, :]))
print("Test X : {}".format(test_X))
print("Test Y : {}".format(test_Y))

N_input = np.reshape(X[0], -1).shape[0]
N_output = np.reshape(Y[0], -1).shape[0]
topo = [N_input, 6, N_output]
alpha = 1
lambdaa = 0.0001
print("nn topo : {}".format(topo))

## Momentum
learning_rate = MomentumLearningRate(learning_rate=alpha, beta=0.9)
Ejemplo n.º 2
0
src = ROOT_DIR+'/../../Data/iris/iris_data.csv'
feature_types = [1,1,1,1,0]
# src = ROOT_DIR+'/../../Data/iris/iris_data.csv'
# feature_types = [1,1,1,1,0]
feature_maker = FeatureMaker(src=src, delimiter=',', types=feature_types, norm=True)
X, Y = feature_maker.make(skip_rows=1, skip_cols=1, one_hot=True)
### 将数据集做成二分类
X = X[Y[:,0]!=1,:]
Y = Y[Y[:,0]!=1,:]
# print(Y)
Y = np.array([[1] if y[0]>1 else [-1] for y in Y])
# Y = Y.flatten()
print("X.shape:{}\nY.shape:{}".format(X.shape, Y.shape))

Xtrain, Xtest, Ytrain, Ytest = feature_maker.train_test_split(X, Y, test_size=0.3)
Xmarked, Xunmarked, Ymarked, Yunmarked = feature_maker.train_test_split(Xtrain, Ytrain, test_size=6.0/7)
Ytest = Ytest.flatten()
Ymarked = Ymarked.flatten()
Yunmarked = Yunmarked.flatten()
print("Xmarked.shape:{} Ymarked.shape:{}\nXunmarked.shape:{} Yunmarked.shape:{}\nXtest.shape:{} Ytest.shape:{}".format(Xmarked.shape, Ymarked, Xunmarked.shape, Yunmarked, Xtest.shape, Ytest))

# sys.exit(0)

def my_hinge(z):
  return max(0.0, np.mean(1.0-z))

### 每次找到最大的两个交换
def assign_labels(pseudo_label, epsilon):
  pos_max = 0.0
  neg_max = 0.0