本文整理汇总了Python中NeuralNetwork.NeuralNetwork.predict方法的典型用法代码示例。如果您正苦于以下问题:Python NeuralNetwork.predict方法的具体用法?Python NeuralNetwork.predict怎么用?Python NeuralNetwork.predict使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NeuralNetwork.NeuralNetwork
的用法示例。
在下文中一共展示了NeuralNetwork.predict方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: NeuralNetwork
# 需要导入模块: from NeuralNetwork import NeuralNetwork [as 别名]
# 或者: from NeuralNetwork.NeuralNetwork import predict [as 别名]
import numpy as np
from NeuralNetwork import NeuralNetwork
nn = NeuralNetwork([2, 2, 1], 'tanh')
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.array([0, 1, 1, 0])
nn.fit(X, y)
for i in [[0, 0], [0, 1], [1, 0], [1, 1]]:
print(i, nn.predict(i))
示例2: load_digits
# 需要导入模块: from NeuralNetwork import NeuralNetwork [as 别名]
# 或者: from NeuralNetwork.NeuralNetwork import predict [as 别名]
# 每个图片8x8 识别数字:0,1,2,3,4,5,6,7,8,9
import numpy as np
from sklearn.datasets import load_digits
from sklearn.metrics import confusion_matrix, classification_report
from sklearn.preprocessing import LabelBinarizer
from NeuralNetwork import NeuralNetwork
from sklearn.cross_validation import train_test_split
digits = load_digits()
X = digits.data
y = digits.target
X -= X.min() # normalize the values to bring them into the range 0-1
X /= X.max()
nn = NeuralNetwork([64, 100, 10], 'logistic')
X_train, X_test, y_train, y_test = train_test_split(X, y)
labels_train = LabelBinarizer().fit_transform(y_train)
labels_test = LabelBinarizer().fit_transform(y_test)
print "start fitting"
nn.fit(X_train, labels_train, epochs=3000)
predictions = []
for i in range(X_test.shape[0]):
o = nn.predict(X_test[i])
predictions.append(np.argmax(o))
print confusion_matrix(y_test, predictions)
print classification_report(y_test, predictions)
示例3: YourFaceSoundsFamiliar
# 需要导入模块: from NeuralNetwork import NeuralNetwork [as 别名]
# 或者: from NeuralNetwork.NeuralNetwork import predict [as 别名]
class YourFaceSoundsFamiliar(BaseWidget):
def __init__(self):
super(YourFaceSoundsFamiliar,self).__init__('Your Face Sounds Familiar')
#Predict Tab
self._imagepath = ControlText('Path')
self._browsebuttonpredict = ControlButton('Browse')
self._nametopred = ControlText('Name')
self._selectfile = ControlFile()
self._selectfile.changed = self.__change_path
self._predictimage = ControlImage()
self._predictbutton = ControlButton('Predict')
self._predicteddetails = ControlLabel('Details')
self._name = ControlLabel('Recognized Name: ')
self._fscore = ControlLabel('FScore: ')
self._predictbutton.value = self.__predictbAction
#Train Tab
self._pername = ControlText('Name')
self._selectdir = ControlDir()
self._selectdir.changed = self.__change_path_dir
self._imagetotrain = ControlImage()
# self._imagetotest = ControlImage()
self._totrainlist = ControlList("To Train",defaultValue=[])
self.traininglist = self._totrainlist.value
self._addtolistbutton = ControlButton('Add')
self._addtolistbutton.value = self.__addtolistbAction
self._trainbutton = ControlButton('Train')
self._trainbutton.value = self.__trainbAction
#Formsets
self._formset = [{
'Predict':['_selectfile','=','_nametopred','=','_predictimage',
'=','_predictbutton','=',
'_predicteddetails','=','_name',
'=','_fscore'],
'Train': ['_pername', '=', '_selectdir',
'=', '_imagetotrain', '=', '_addtolistbutton','=' ,
'_totrainlist', '=', '_trainbutton']
}]
self.trainingsetall = []
self.nn = self.__init_nn()
self.learned = {}
self._k = 4
self._trainingPercent = 0.8
self.learned = self.__load_learned()
self.cross_validation_set = [np.empty((0,0))]*self._k
self.cross_validation_set_y = [np.empty((0,0))]*self._k
self.test_set = np.empty((0, 0))
self.testing_y = np.empty((0, 0))
self.training_X = [np.empty((0, 900))] * self._k
self.training_y = [np.empty((0, 1))] * self._k
self.X = np.empty((0, 0))
def __load_learned(self):
try:
with open('learned.json') as learned_file:
for line in learned_file:
learned = json.loads(line)
for key in learned.keys():
self._totrainlist.__add__([key])
except IOError:
learned = {}
config = {'input_size': 30 * 30, 'hidden_size': 30 * 30, 'lambda': 1, 'num_labels': (len(learned))}
self.nn = NeuralNetwork(config=config)
return learned
def __predictbAction(self):
predictset_filename = 'predictset.csv'
np.savetxt(predictset_filename,self.predictset, delimiter=',')
prediction = np.argmax(self.nn.predict(self.predictset)) + 1
for k, v in self.learned.iteritems():
if prediction == v:
self._name.value = k
def __init_nn(self):
nn = NeuralNetwork()
return nn
def __change_path(self):
image = cv2.imread(self._selectfile.value)
self._predictimage.value = []
self._predictimage.value = FaceDetection().drawrectangle(image)
resizedimage = FaceDetection().resizeimageb(self._predictimage.value)
croppedimage = FaceDetection().cropface(resizedimage)
resizedcroppedimage = FaceDetection().resizeimagea(croppedimage)
self.predictset = np.array(resizedcroppedimage[1]).flatten()
def __change_path_dir(self):
name = self._selectdir.value
name = name.split('/')
self._pername.value = name.pop(len(name)-1)
self._imagetotrain.value = []
# self._imagetotest.value = []
listofimages = os.listdir(self._selectdir.value)
listofimages = sorted(listofimages)
#.........这里部分代码省略.........
示例4: load_digits
# 需要导入模块: from NeuralNetwork import NeuralNetwork [as 别名]
# 或者: from NeuralNetwork.NeuralNetwork import predict [as 别名]
# import pylab as pl
# pl.gray()
# pl.matshow(digits.images[0])
# pl.show()
from sklearn.preprocessing import LabelBinarizer
from NeuralNetwork import NeuralNetwork
from sklearn.cross_validation import train_test_split
from sklearn.datasets import load_digits
import numpy as np
from sklearn.metrics import confusion_matrix,classification_report
digits = load_digits()
x= digits.data
y = digits.target
x -= x.min()
x /= x.max()
nn = NeuralNetwork([64,100,10],"logistic")
x_train,x_test,y_train,y_test = train_test_split(x,y)
label_train = LabelBinarizer().fit_transform(y_train)
label_test = LabelBinarizer().fit_transform(y_test)
print("start fitting..")
predictions = []
nn.fit(x_train, label_train, epochs=10000)
for i in range(x_test.shape[0]):
o = nn.predict(x_test[i])
predictions.append(np.argmin(o))
print confusion_matrix(y_test,predictions)
print classification_report(y_test,predictions)
示例5: contrast_normalize
# 需要导入模块: from NeuralNetwork import NeuralNetwork [as 别名]
# 或者: from NeuralNetwork.NeuralNetwork import predict [as 别名]
# Assumes "train.mat" is the training set from MNIST
train_data = sc.io.loadmat('dataset/train.mat')
train_images = train_data['train_images']
train_labels = train_data['train_labels']
side_length = train_images.shape[0]
preprocessed_images = np.transpose(train_images.reshape((side_length*side_length,-1)))
preprocessed_images = contrast_normalize(preprocessed_images)
training_features, training_labels, validation_features, validation_labels = split_data(preprocessed_images, train_labels, 1/6.0)
test_data = sc.io.loadmat('dataset/test.mat')
test_images = test_data['test_images']
preprocessed_test_images = test_images.reshape((10000, 784))
preprocessed_test_images = contrast_normalize(preprocessed_test_images)
# This actually isn't a great setup for MNIST, multiple hidden layers aren't useful unless you're doing convolutions.
example_net = NeuralNetwork(cost_func = cross_entropy, cost_deriv = cross_entropy_deriv,
activation_func = ReLU, activ_deriv = ReLU_deriv,
output_func = softmax, output_deriv = softmax_deriv,
hid_layer_sizes=[200,200], num_inputs = 784, num_outputs=10, learning_rate = 1e-2, stopping_threshold=-1,
momentum_rate = 0.9, batch_size = 50, decay_rate = 0.5, decay_frequency = 20,
cost_calc_freq = 1000, snapshot_frequency = -1,
snapshot_name = "./snapshots/multilayer_softmax_ReLU", max_iterations = 1e6, relax_targets=False)
example_net.train(training_features, training_labels)
validation_predictions = example_net.predict(validation_features)
benchmark(validation_predictions,validation_labels)
final_predictions = example_net.predict(preprocessed_test_images)
示例6: __init__
# 需要导入模块: from NeuralNetwork import NeuralNetwork [as 别名]
# 或者: from NeuralNetwork.NeuralNetwork import predict [as 别名]
#.........这里部分代码省略.........
print "Momentum factor :", momentumFactor
print "# of Nodes in all layers :", nodeNum
print "Training iteration so far:", totalIter
self.file.write("\n")
self.file.write("---------- Settings ----------" + "\n")
self.file.write("Examples : " + str(training_data.shape[0]) + "\n")
self.file.write("Batch size : " + str(batchSize) + "\n")
self.file.write("Alpha : " + str(self.clf.getAlpha()) + "\n")
self.file.write("Momentum factor : " + str(momentumFactor) + "\n")
self.file.write("# of Nodes in all layers : " + str(nodeNum) + "\n")
self.file.write("Training iteration so far: " + str(totalIter) + "\n")
self.test(training_data, "training")
self.test(testData, "testing")
iteration = 0
print ""
restart = raw_input("Do you want to restart? (Y/N)")
if restart.upper() == "Y":
totalIter = 0
print "Current Alpha is", self.clf.getAlpha()
alpha = raw_input("What alpha ?")
self.clf.setAlpha(float(alpha))
self.clf.initTheta()
self.file.write("\n")
self.file.write("*****************************************************\n")
self.file.write("Re-initialize trail with alpha = " + str(alpha) + "\n")
self.file.write("*****************************************************\n")
print ""
iteration = raw_input("How many iteration do you want to train the model?")
try:
iteration = int(iteration)
except:
iteration = raw_input("Please input an integer")
iteration = 1
print "Total training iterations:", totalIter
def predict(self, data):
"""
"""
return self.clf.predict(data)
def test(self, test_data, mode):
"""
"""
correct = 0
countPrediction = {}
countCorrect = {}
countTotal = Counter(list(test_data[:, 0]))
allPrediction = {}
labels = np.unique(test_data[:, 0])
for label in labels:
countCorrect[label] = 0
countPrediction[label] = 0
allPrediction[label] = 0
for e in test_data:
label = e[0]
pred_label = self.predict(e)
if label == pred_label:
correct += 1
if e[0] in countCorrect:
countCorrect[e[0]] += 1
else:
countCorrect[e[0]] = 1
if pred_label in allPrediction:
allPrediction[pred_label] += 1
else:
allPrediction[pred_label] = 1
if pred_label in countPrediction:
countPrediction[pred_label] += 1
else:
countPrediction[pred_label] = 1
print "---------- Result ----------"
print "Alpha is", self.clf.getAlpha()
print "Count correct", countCorrect
print "All predictions", allPrediction
accuracy = float(correct) / len(test_data)
print "The accuracy for", mode, "is", accuracy
self.file.write("---------- Result ----------" + "\n")
self.file.write("Alpha is " + str(self.clf.getAlpha()) + "\n")
self.file.write("Count correct " + str(countCorrect) + "\n")
self.file.write("All predictions " + str(allPrediction) + "\n")
self.file.write("The accuracy for " + mode + " is " + str(accuracy) + "\n")
def getAttrValue(self, ex):
"""
Find the attribute values for each attribute.
Args:
ex: given examples
Returns: a dictionary where the keys are the attribute indices and the values are the attribute values.
"""
attrValue = {}
for i in range(len(ex[0])):
attrValue[i] = list(set([v for v in ex[:, i]]))
return attrValue
示例7: test_classification
# 需要导入模块: from NeuralNetwork import NeuralNetwork [as 别名]
# 或者: from NeuralNetwork.NeuralNetwork import predict [as 别名]
def test_classification():
from sklearn.datasets import load_digits
from sklearn.datasets import load_iris
from sklearn.cross_validation import train_test_split
from sklearn.preprocessing import LabelBinarizer
from sklearn.metrics import confusion_matrix, classification_report, accuracy_score
digits = load_digits()
iris = load_iris()
breast = dt.load_breast_cancer()
ocr = dt.load_ocr_train()
ocr1 = dt.load_ocr_test()
X = digits.data
y = digits.target
X -= X.min() # normalize the values to bring them into the range 0-1
X /= X.max()
X_train, X_test, y_train, y_test = train_test_split(X, y)
labels_train = LabelBinarizer().fit_transform(y_train)
labels_test = LabelBinarizer().fit_transform(y_test)
print 'digits dataset'
print 'MLP performance:'
mlp = MLPClassifier()
mlp.fit(X_train,labels_train)
predictions = []
for i in range(X_test.shape[0]):
o = mlp.predict(X_test[i] )
predictions.append(np.argmax(o))
print confusion_matrix(y_test,predictions)
print classification_report(y_test,predictions)
print 'Perceptron performance'
nn = NeuralNetwork([64,100,10],'tanh')
nn.fit(X_train,labels_train,epochs=100)
predictions = []
for i in range(X_test.shape[0]):
o = nn.predict(X_test[i] )
predictions.append(np.argmax(o))
print confusion_matrix(y_test,predictions)
print classification_report(y_test,predictions)
#################################################
X = iris.data
y = iris.target
#X -= X.min() # normalize the values to bring them into the range 0-1
#X /= X.max()
X_train, X_test, y_train, y_test = train_test_split(X, y)
labels_train = LabelBinarizer().fit_transform(y_train)
labels_test = LabelBinarizer().fit_transform(y_test)
print 'Iris dataset'
print 'MLP performance'
mlp = MLPClassifier()
mlp.fit(X_train,labels_train)
predictions = []
for i in range(X_test.shape[0]):
o = mlp.predict(X_test[i] )
predictions.append(np.argmax(o))
print confusion_matrix(y_test,predictions)
print classification_report(y_test,predictions)
print 'Perceptron performance'
nn = NeuralNetwork([64,100,10],'tanh')
nn.fit(X_train,labels_train,epochs=100)
predictions = []
for i in range(X_test.shape[0]):
o = nn.predict(X_test[i] )
predictions.append(np.argmax(o))
print confusion_matrix(y_test,predictions)
print classification_report(y_test,predictions)
####################################################
X_train = breast['x_train']
y_train = breast['y_train']
X_test = breast['x_test']
y_test = breast['y_test']
X_train -= X_train.min() # normalize the values to bring them into the range 0-1
X_train /= X_train.max()
labels_train = LabelBinarizer().fit_transform(y_train)
labels_test = LabelBinarizer().fit_transform(y_test)
print 'Breast cancer dataset'
print 'MLP performance'
mlp = MLPClassifier()
mlp.fit(X_train,labels_train)
predictions = []
for i in range(X_test.shape[0]):
o = mlp.predict(X_test[i] )
predictions.append(np.argmax(o))
print accuracy_score(labels_test,predictions)
#print confusion_matrix(labels_test,predictions)
print classification_report(labels_test,predictions)
print 'Perceptron performance'
nn = NeuralNetwork([64,100,10],'tanh')
nn.fit(X_train,labels_train,epochs=100)
predictions = []
for i in range(X_test.shape[0]):
o = nn.predict(X_test[i] )
predictions.append(np.argmax(o))
print confusion_matrix(labels_test,predictions)
print classification_report(labels_test,predictions)
####################################################
'''