本文整理匯總了Python中pybrain.tools.customxml.NetworkReader類的典型用法代碼示例。如果您正苦於以下問題:Python NetworkReader類的具體用法?Python NetworkReader怎麽用?Python NetworkReader使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了NetworkReader類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
def __init__(self, mat, cmap=None, pixelspervalue=20, minvalue=None, maxvalue=None, show=True, block=False):
""" Make a colormap image of a matrix or sequence of Matrix/Connection objects
:key mat: the matrix to be used for the colormap.
:key cmap: the matplotlib colormap (color scale) to use ('hot', 'hot_r', 'gray', 'gray_r', 'hsv', 'prism', pylab.cm.hot, etc)
"""
self.colormaps = []
if isinstance(mat, basestring):
try:
#nn = NetworkReader(mat, newfile=False)
mat = NetworkReader(mat, newfile=False).readFrom(mat)
except:
pass
try: # if isinstance(mat, Trainer):
mat = mat.module
except:
pass
if isinstance(mat, Network):
# connections is a dict with key: value pairs of Layer: Connection (ParameterContainer)
mat = [connection for connection in mat.connections.values() if connection]
# connections = mat.module.connections.values()
# mat = []
# for conlist in connections:
# mat += conlist
try:
mat = [v for (k, v) in mat.iteritems()]
if not any(isinstance(m, (ParameterContainer, Connection)) for m in mat):
raise ValueError("Don't know how to display ColorMaps for a sequence of type {} containing key, values of type {}: {}".format(
type(mat), *[type(m) for m in mat.iteritems().next()]))
except AttributeError:
pass
# from traceback import print_exc
# print_exc()
if isinstance(mat, list):
for m in mat:
if isinstance(m, list):
if len(m) == 1:
m = m[0]
else:
raise ValueError("Don't know how to display a ColorMap for a list containing more than one matrix: {}".format([type(m) for m in mat]))
try:
self.colormaps = [ColorMap(m, cmap=cmap, pixelspervalue=pixelspervalue, minvalue=minvalue, maxvalue=maxvalue) ]
except ValueError:
self.colormaps = [ColorMap(m[0], cmap=cmap, pixelspervalue=pixelspervalue, minvalue=minvalue, maxvalue=maxvalue) ]
else:
self.colormaps = [ColorMap(mat)]
# raise ValueError("Don't know how to display ColorMaps for a sequence of type {}".format(type(mat)))
if show:
self.show(block=block)
示例2: __init__
def __init__(self, name, deck_id):
self.neural_network = NetworkReader.readFrom('network.xml')
hero = get_hero(deck_id)
self.deck_id = deck_id
self.original_deck = get_deck_by_id(deck_id)
super(Q_player, self).__init__(name, self.original_deck, hero)
示例3: classify
def classify(data,network_file='network_training_progress.xml'):
"""
Takes two arguments, 'data' is a 1D array of floats ranging 0-1 representing grayscale values of an image,
'network_file' is an xml file output from 'pybrain_playground.py', a pre-trained network.
Returns two floats, how much it guesses that a given input is a track or other, respectively.
Again, classify()[0] is chances it is a track, classify()[1] is chances it is other. Ranged 0-1.
Example:
>>> im = loadImage('path/to/track.png')
>>> print classify(im)[0]
0.99
>>> print classify(im)[1]
0.01
Here, 0.99 indicates it believes with 99% certainty that the image is a track,
and 0.01% certainty that it is not a track.
!!!IMPORTANT!!!
THE PROGRAM EXPECTS FILES OUTPUT FROM PREPROCESSING.PY,
AND PREPREOCESSING.PY EXPECTS FILES THAT HAVE BEEN
OUTPUT BY PLOTBLOBS.PY
!!!IMPORTANT!!!
"""
net = NetworkReader.readFrom(network_file)
return net.activate(data)
示例4: test
def test(filename, test_data):
ann = NetworkReader.readFrom(filename)
#file = open('results.csv', 'w', newline = ' ')
rank_teams = {1: 'Philadelphia 76ers', 2: 'Los Angeles Lakers', 3: 'Brooklyn Nets', 4: 'Phoenix Suns',
5: 'Minnesota Timberwolves', 6: 'New Orleans Pelicans', 7: 'New York Knicks',
8: 'Sacramento Kings', 9: 'Milwaukee Bucks', 10: 'Denver Nuggets', 11: 'Orlando Magic',
12: 'Utah Jazz', 13: 'Washington Wizards', 14: 'Houston Rockets', 15: 'Chicago Bulls',
16: 'Memphis Grizzlies', 17: 'Dallas Mavericks', 18: 'Portland Trail Blazers',
19: 'Detroit Pistons', 20: 'Indiana Pacers', 21: 'Miami Heat',
22: 'Charlotte Hornets', 23: 'Boston Celtics', 24: 'Atlanta Hawks',
25: 'Los Angeles Clippers', 26: 'Oklahoma City Thunder', 27: 'Toronto Raptors',
28: 'Cleveland Cavaliers', 29: 'San Antonio Spurs', 30: 'Golden State Warriors'}
list_ = []
with open('temp_file2.csv', 'w', newline = '') as fp:
temp = csv.writer(fp)
for i in range(1,31):
for j in range(1, 31):
if(i != j):
out = ann.activate([i, j, 0, 0, 0])
if (out > 1.00):
out = 99
else:
num = out * 100
out = int(num)
temp.writerow([rank_teams.get(i), rank_teams.get(j), out])
示例5: main
def main():
start_time = time.time()
novice = ArtificialNovice()
genius = ArtificialGenius()
game = HangmanGame(genius, novice)
if __debug__:
print "------------------- EVALUATION ------------------------"
network = NetworkReader.readFrom("../IA/network_weight_1000.xml")
j = 0
while j < 1:
game.launch(False, None, network)
j += 1
print ("--- %s total seconds ---" % (time.time() - start_time))
else:
print "------------------- LEARNING ------------------------"
network = buildNetwork(3, 4, 1, hiddenclass=SigmoidLayer)
ds = SupervisedDataSet(3, 1)
i = 0
while i < 100:
game.launch(True, ds)
i += 1
print " INITIATE trainer : "
trainer = BackpropTrainer(network, ds)
print " START trainer : "
start_time_trainer = time.time()
trainer.train()
print ("--- END trainer in % seconds ---" % (time.time() - start_time_trainer))
print " START EXPORT network : "
NetworkWriter.writeToFile(network, "../IA/network_weight_test_learning.xml")
print " END EXPORT network : "
示例6: runSaveNet
def runSaveNet(netName):
net = NetworkReader.readFrom(netName)
print '0,0,0->', net.activate([0,0,0])
print '0,0,1->', net.activate([0,0,1])
print '0,1,0->', net.activate([0,1,0])
print '0,1,1->', net.activate([0,1,1])
print '1,0,0->', net.activate([1,0,0])
print '1,0,1->', net.activate([1,0,1])
print '1,1,0->', net.activate([1,1,0])
print '1,1,1->', net.activate([1,1,1])
print "-----------------------------------------------------"
print 'Max position of 0,0,0->', getMaxPosition(net.activate([0,0,0])) + 1
print 'Max position of 0,0,1->', getMaxPosition(net.activate([0,0,1])) + 1
print 'Max position of 0,1,0->', getMaxPosition(net.activate([0,1,0])) + 1
print 'Max position of 0,1,1->', getMaxPosition(net.activate([0,1,1])) + 1
print 'Max position of 1,0,0->', getMaxPosition(net.activate([1,0,0])) + 1
print 'Max position of 1,0,1->', getMaxPosition(net.activate([1,0,1])) + 1
print 'Max position of 1,1,0->', getMaxPosition(net.activate([1,1,0])) + 1
print 'Max position of 1,1,1->', getMaxPosition(net.activate([1,1,1])) + 1
print
print
示例7: loadFromDir
def loadFromDir(cls, dirPath):
"""
Return a classifier, loaded from the given directory.
"""
with codecs.open(os.path.join(dirPath, cls._CLASSIFIER_NAME), encoding='utf-8') as f:
c = serializer.load(f.read())
c.net = NetworkReader.readFrom(os.path.join(dirPath, cls._NET_NAME))
return c
示例8: xmlInvariance
def xmlInvariance(n, forwardpasses = 1):
""" try writing a network to an xml file, reading it, rewrite it, reread it, and compare
if the result looks the same (compare string representation, and forward processing
of some random inputs) """
# We only use this for file creation.
tmpfile = tempfile.NamedTemporaryFile(dir='.')
f = tmpfile.name
tmpfile.close()
NetworkWriter.writeToFile(n, f)
tmpnet = NetworkReader.readFrom(f)
NetworkWriter.writeToFile(tmpnet, f)
endnet = NetworkReader.readFrom(f)
# Unlink temporary file.
os.unlink(f)
netCompare(tmpnet, endnet, forwardpasses, True)
示例9: main
def main():
print "Calculating mfcc...."
mfcc_coeff_vectors_dict = {}
for i in range(1, 201):
extractor = FeatureExtractor(
'/home/venkatesh/Venki/FINAL_SEM/Project/Datasets/Happiness/HappinessAudios/' + str(i) + '.wav')
mfcc_coeff_vectors = extractor.calculate_mfcc()
mfcc_coeff_vectors_dict.update({str(i): (mfcc_coeff_vectors, mfcc_coeff_vectors.shape[0])})
for i in range(201, 401):
extractor = FeatureExtractor(
'/home/venkatesh/Venki/FINAL_SEM/Project/Datasets/Sadness/SadnessAudios/' + str(i - 200) + '.wav')
mfcc_coeff_vectors = extractor.calculate_mfcc()
mfcc_coeff_vectors_dict.update({str(i): (mfcc_coeff_vectors, mfcc_coeff_vectors.shape[0])})
audio_with_min_frames, min_frames = get_min_frames_audio(
mfcc_coeff_vectors_dict)
processed_mfcc_coeff = preprocess_input_vectors(
mfcc_coeff_vectors_dict, min_frames)
# frames = min_frames
# print frames
# print len(processed_mfcc_coeff['1'])
# for each_vector in processed_mfcc_coeff['1']:
# print len(each_vector)
print "mffcc found..."
classes = ["happiness", "sadness"]
training_data = ClassificationDataSet(
26, target=1, nb_classes=2, class_labels=classes)
# training_data = SupervisedDataSet(13, 1)
try:
network = NetworkReader.readFrom(
'network_state_frame_level_new2_no_pp1.xml')
except:
for i in range(1, 51):
mfcc_coeff_vectors = processed_mfcc_coeff[str(i)]
for each_vector in mfcc_coeff_vectors:
training_data.appendLinked(each_vector, [1])
for i in range(201, 251):
mfcc_coeff_vectors = processed_mfcc_coeff[str(i)]
for each_vector in mfcc_coeff_vectors:
training_data.appendLinked(each_vector, [0])
training_data._convertToOneOfMany()
print "prepared training data.."
print training_data.indim, training_data.outdim
network = buildNetwork(
training_data.indim, 5, training_data.outdim, fast=True)
trainer = BackpropTrainer(network, learningrate=0.01, momentum=0.99)
print "Before training...", trainer.testOnData(training_data)
trainer.trainOnDataset(training_data, 1000)
print "After training...", trainer.testOnData(training_data)
NetworkWriter.writeToFile(
network, "network_state_frame_level_new2_no_pp.xml")
示例10: __init__
def __init__(self, inpNeurons, hiddenNeurons, outNeurons):
self.net = buildNetwork(inpNeurons, hiddenNeurons, outNeurons, hiddenclass=TanhLayer, bias=True)
if raw_input('Recover Network?: y/n\n')=='y':
print 'Recovering Network'
net = NetworkReader.readFrom('Network1.xml')
else:
print 'New Network'
self.net.randomize()
print self.net
self.ds = SupervisedDataSet(inpNeurons,outNeurons)
self.trainer = BackpropTrainer(self.net, self.ds, learningrate = 0.01, momentum=0.99)
示例11: __init__
def __init__(self, name, deck_id, neural_net):
if (neural_net == None):
path = os.path.join(os.path.dirname(os.getcwd()), 'network.xml')
self.neural_network = NetworkReader.readFrom(path)
else:
self.neural_network = neural_net
hero = get_hero(deck_id)
self.deck_id = deck_id
self.original_deck = get_deck_by_id(deck_id)
super(Q_learner, self).__init__(name, self.original_deck, hero)
示例12: weight_matrices
def weight_matrices(nn):
""" Extract list of weight matrices from a Network, Layer (module), Trainer, Connection or other pybrain object"""
if isinstance(nn, ndarray):
return nn
try:
return weight_matrices(nn.connections)
except:
pass
try:
return weight_matrices(nn.module)
except:
pass
# Network objects are ParameterContainer's too, but won't reshape into a single matrix,
# so this must come after try nn.connections
if isinstance(nn, (ParameterContainer, Connection)):
return reshape(nn.params, (nn.outdim, nn.indim))
if isinstance(nn, basestring):
try:
fn = nn
nn = NetworkReader(fn, newfile=False)
return weight_matrices(nn.readFrom(fn))
except:
pass
# FIXME: what does NetworkReader output? (Module? Layer?) need to handle it's type here
try:
return [weight_matrices(v) for (k, v) in nn.iteritems()]
except:
try:
connections = nn.module.connections.values()
nn = []
for conlist in connections:
nn += conlist
return weight_matrices(nn)
except:
return [weight_matrices(v) for v in nn]
示例13: main
def main():
print "Calculating mfcc...."
mfcc_coeff_vectors_dict = {}
for i in range(1, 201):
extractor = FeatureExtractor('/home/venkatesh/Venki/FINAL_SEM/Project/Datasets/Happiness/HappinessAudios/' + str(i) + '.wav')
mfcc_coeff_vectors = extractor.calculate_mfcc()
mfcc_coeff_vectors_dict.update({str(i): (mfcc_coeff_vectors, mfcc_coeff_vectors.shape[0])})
for i in range(201, 401):
extractor = FeatureExtractor('/home/venkatesh/Venki/FINAL_SEM/Project/Datasets/Sadness/SadnessAudios/' + str(i - 200) + '.wav')
mfcc_coeff_vectors = extractor.calculate_mfcc()
mfcc_coeff_vectors_dict.update({str(i): (mfcc_coeff_vectors, mfcc_coeff_vectors.shape[0])})
audio_with_min_frames, min_frames = get_min_frames_audio(mfcc_coeff_vectors_dict)
processed_mfcc_coeff = preprocess_input_vectors(mfcc_coeff_vectors_dict, min_frames)
frames = min_frames
print "mfcc found...."
classes = ["happiness", "sadness"]
try:
network = NetworkReader.readFrom('network_state_new_.xml')
except:
# Create new network and start Training
training_data = ClassificationDataSet(frames * 26, target=1, nb_classes=2, class_labels=classes)
# training_data = SupervisedDataSet(frames * 39, 1)
for i in range(1, 151):
mfcc_coeff_vectors = processed_mfcc_coeff[str(i)]
training_data.appendLinked(mfcc_coeff_vectors.ravel(), [1])
# training_data.addSample(mfcc_coeff_vectors.ravel(), [1])
for i in range(201, 351):
mfcc_coeff_vectors = processed_mfcc_coeff[str(i)]
training_data.appendLinked(mfcc_coeff_vectors.ravel(), [0])
# training_data.addSample(mfcc_coeff_vectors.ravel(), [0])
training_data._convertToOneOfMany()
network = buildNetwork(training_data.indim, 5, training_data.outdim)
trainer = BackpropTrainer(network, learningrate=0.01, momentum=0.99)
print "Before training...", trainer.testOnData(training_data)
trainer.trainOnDataset(training_data, 1000)
print "After training...", trainer.testOnData(training_data)
NetworkWriter.writeToFile(network, "network_state_new_.xml")
print "*" * 30 , "Happiness Detection", "*" * 30
for i in range(151, 201):
output = network.activate(processed_mfcc_coeff[str(i)].ravel())
# print output,
# if output > 0.7:
# print "happiness"
class_index = max(xrange(len(output)), key=output.__getitem__)
class_name = classes[class_index]
print class_name
示例14: import_network
def import_network(self, filename):
train_samples = self.samples
train_labels = self.labels
np.random.seed(0)
np.random.shuffle(train_samples)
np.random.seed(0)
np.random.shuffle(train_labels)
self.net_shared = NetworkReader.readFrom(filename)
self.ds_shared = SupervisedDataSet(300, 1)
for i in range(len(train_samples)):
self.ds_shared.addSample(tuple(np.array(train_samples[i], dtype='float64')), (train_labels[i],))
self.trainer_shared = BackpropTrainer(self.net_shared, self.ds_shared, verbose=True)
示例15: neural_predict
def neural_predict(filename, train_file, output):
testtag, testdata = readfile(filename)
net = NetworkReader.readFrom(train_file)
i = 0
num = 0
output_file = open(output, 'w')
output_file.write("test data size: " + str(len(testtag)) + '\n')
output_type_list = []
output_type_size = []
output_type_right = []
output_typt_error_detail = []
for k in testdata:
res = net.activate(k)
if testtag[i] not in output_type_list:
output_type_list.append(testtag[i])
output_type_size.append(0)
output_type_right.append(0)
output_typt_error_detail.append([])
j = output_type_list.index(testtag[i])
output_type_size[j] += 1
if labals[max_index(res)] == testtag[i]:
num += 1
output_type_right[j] += 1
else:
(output_typt_error_detail[j]).append(labals[max_index(res)])
i += 1
# for k in testdata:
# res = net.activate(k)
# if labals[max_index(res)] == testtag[i]:
# num += 1
# i += 1
output_file.write("correct number: " + str(num) + '\n')
output_file.write("correct rate: " + str(num / (float)(len(testtag))) + '\n')
i = 0
for x in output_type_list:
output_file.write(x + "\t")
output_file.write(str(output_type_right[i]) + '/' + str(output_type_size[i]) + ':'
+ str(float(output_type_right[i]) / output_type_size[i])[0:5] + '\t')
c = Counter(output_typt_error_detail[i])
for y in c:
output_file.write(y + ":" + str(c[y]) + '\t')
print(y + ":" + str(c[y]))
# print c
i += 1
output_file.write('\n')
print num
output_file.close()