本文整理汇总了Python中pybrain.structure.networks.feedforward.FeedForwardNetwork.activate方法的典型用法代码示例。如果您正苦于以下问题:Python FeedForwardNetwork.activate方法的具体用法?Python FeedForwardNetwork.activate怎么用?Python FeedForwardNetwork.activate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pybrain.structure.networks.feedforward.FeedForwardNetwork
的用法示例。
在下文中一共展示了FeedForwardNetwork.activate方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from pybrain.structure.networks.feedforward import FeedForwardNetwork [as 别名]
# 或者: from pybrain.structure.networks.feedforward.FeedForwardNetwork import activate [as 别名]
def main():
a = 0
for i in range(0,100):
inLayer = SigmoidLayer(2)
hiddenLayer = SigmoidLayer(3)
outLayer = SigmoidLayer(1)
net = FeedForwardNetwork()
net.addInputModule(inLayer)
net.addModule(hiddenLayer)
net.addOutputModule(outLayer)
in_to_hidden = FullConnection(inLayer,hiddenLayer)
hidden_to_out = FullConnection(hiddenLayer,outLayer)
net.addConnection(in_to_hidden)
net.addConnection(hidden_to_out)
net.sortModules()
ds = SupervisedDataSet(2,1)
ds.addSample((1,1), (0))
ds.addSample((1,0), (1))
ds.addSample((0,1), (1))
ds.addSample((0,0), (0))
trainer = BackpropTrainer(net,ds)
trainer.trainUntilConvergence()
out = net.activate((1,1))
if (out < 0.5):
a = a + 1
print(str(a) + "/100")
示例2: __init__
# 需要导入模块: from pybrain.structure.networks.feedforward import FeedForwardNetwork [as 别名]
# 或者: from pybrain.structure.networks.feedforward.FeedForwardNetwork import activate [as 别名]
class PyBrainANNs:
def __init__(self, x_dim, y_dim, hidden_size, s_id):
self.serialize_id = s_id
self.net = FeedForwardNetwork()
in_layer = LinearLayer(x_dim)
hidden_layer = SigmoidLayer(hidden_size)
out_layer = LinearLayer(y_dim)
self.net.addInputModule(in_layer)
self.net.addModule(hidden_layer)
self.net.addOutputModule(out_layer)
in_to_hidden = FullConnection(in_layer, hidden_layer)
hidden_to_out = FullConnection(hidden_layer, out_layer)
self.net.addConnection(in_to_hidden)
self.net.addConnection(hidden_to_out)
self.net.sortModules()
def _prepare_dataset(self, x_data, y_data):
assert x_data.shape[0] == y_data.shape[0]
if len(y_data.shape) == 1:
y_matrix = np.matrix(y_data).T
else:
y_matrix = y_data.values
assert x_data.shape[1] == self.net.indim
assert y_matrix.shape[1] == self.net.outdim
data_set = SupervisedDataSet(self.net.indim, self.net.outdim)
data_set.setField("input", x_data)
data_set.setField("target", y_matrix)
return data_set
def train(self, x_data, y_data):
trainer = BackpropTrainer(self.net, self._prepare_dataset(x_data, y_data))
trainer.train()
def score(self, x_data, y_datas):
return ModuleValidator.validate(regression_score, self.net, self._prepare_dataset(x_data, y_datas))
def predict(self, x_data):
return np.array([self.net.activate(sample) for sample in x_data])
def save(self, path):
joblib.dump(self.net, path)
def load(self, path):
self.net = joblib.load(path)
示例3: range
# 需要导入模块: from pybrain.structure.networks.feedforward import FeedForwardNetwork [as 别名]
# 或者: from pybrain.structure.networks.feedforward.FeedForwardNetwork import activate [as 别名]
for cycle in range(100):
datafile = 'top1000.data';
for entry in dictionary(datafile):
#print("working on", entry);
outmatrix = outputUnits(entry);
lpos = 0;
ds = SupervisedDataSet(NUMINPUTS, NUMOUTPUTS);
for letterContexts in wordstream(input_entries = (entry,)):
#print("letterContexts", letterContexts);
for inarray in convertToBinary(letterContexts):
outarray = outmatrix[lpos];
#print("inarray",inarray);
#print("outarray",outarray);
#print("inlen %d outlen %d" % (len(inarray), len(outarray)));
ds.addSample(inarray, outarray);
observed = net.activate(inarray);
phoneme = entry.phonemes[lpos];
observedPhoneme = closestByDotProduct(observed[:MINSTRESS], articFeatures);
phonemeErrors.append(bool(phoneme != observedPhoneme));
stress = entry.stress[lpos];
observedStress = closestByDotProduct(observed[MINSTRESS:], stressFeatures);
stressErrors.append(bool(stress != observedStress));
lpos += 1
trainer.setData(ds);
#pdb.set_trace();
err = trainer.train();
#print(err, " ", entry);
print("accuracy: phonemes %.3f stresses %.3f" % (1 - np.mean(phonemeErrors), 1 - np.mean(stressErrors)) );
#accuracy is a vector with one element in {0,1} for each letter i
示例4: percentError
# 需要导入模块: from pybrain.structure.networks.feedforward import FeedForwardNetwork [as 别名]
# 或者: from pybrain.structure.networks.feedforward.FeedForwardNetwork import activate [as 别名]
#outresult = percentError(trainer.testOnClassData(dataset=out),
#tstdata['class'])
#print " out error: %5.4f%%" % outresult
#f = open(r'd:\rrr.csv','w')
#for i in range(len(test)):
# f.write('%d,%d,%f\n' % (items[i][0],items[i][1],out[i]))
#f.close()
#pass
NetworkWriter.writeToFile(n, 'filename.xml')
reader = BinReader(ur'F:\AliRecommendHomeworkData\1212新版\test18.expand.norm.bin')
reader.open()
result = [0] * reader.LineCount
for i in xrange(reader.LineCount):
(x,userid,itemid,label) = reader.readline()
x[0] = 1
y = n.activate(x)[0]
result[i] = (userid,itemid,y)
if i % 10000 == 0:
print '%d/%d' % (i,reader.LineCount)
result.sort(key=lambda x:x[2],reverse=True)
result = result[:7000]
print ur'正在输出...'
with open('result.csv','w') as f:
for item in result:
f.write('%d,%d\n' % (item[0],item[1]))
print ur'阈值:',result[-1][2]
print ur'样本总数:',reader.LineCount
示例5: phoneme_to_layer
# 需要导入模块: from pybrain.structure.networks.feedforward import FeedForwardNetwork [as 别名]
# 或者: from pybrain.structure.networks.feedforward.FeedForwardNetwork import activate [as 别名]
#.........这里部分代码省略.........
self.phonemes_to_layers[phoneme] = layer
def _generate_pybrain_network(self):
# make network
self._pybrain_network = FeedForwardNetwork()
# make layers
self._in_layer = LinearLayer(self.n_input_neurons, name='in')
self._hidden_layer = SigmoidLayer(self.n_hidden_neurons, name='hidden')
self._out_layer = LinearLayer(self.n_output_neurons, name='out')
self._bias_neuron = BiasUnit(name='bias')
# make connections between layers
self._in_hidden_connection = FullConnection(self._in_layer, self._hidden_layer)
self._hidden_out_connection = FullConnection(self._hidden_layer, self._out_layer)
self._bias_hidden_connection = FullConnection(self._bias_neuron, self._hidden_layer)
self._bias_out_connection = FullConnection(self._bias_neuron, self._out_layer)
# add modules to network
self._pybrain_network.addInputModule(self._in_layer)
self._pybrain_network.addModule(self._hidden_layer)
self._pybrain_network.addOutputModule(self._out_layer)
self._pybrain_network.addModule(self._bias_neuron)
# add connections to network
for c in (self._in_hidden_connection, self._hidden_out_connection, self._bias_hidden_connection, self._bias_out_connection):
self._pybrain_network.addConnection(c)
# initialize network with added modules/connections
self._pybrain_network.sortModules()
def windowIter(self, letters):
assert type(letters) == str
padding_before = ' ' * self.window_middle
padding_after = ' ' * (self.window_size - self.window_middle - 1)
padded_letters = padding_before + letters + padding_after
# for each letter in the sample
for l_num in range(len(letters)):
letters_window = padded_letters[l_num:l_num+self.window_size]
yield letters_window
def generateSamples(self, letters, phonemes):
assert len(letters) == len(phonemes)
for (letters_window, current_phoneme) in izip(self.windowIter(letters), phonemes):
yield self.letters_to_layer(letters_window), self.phoneme_to_layer(current_phoneme)
def letters_to_layer(self, letters):
assert len(letters) == self.window_size
# start with empty layer
layer = zeros(self.n_input_neurons)
# loop through letters and activate each neuron
for (pos, letter) in enumerate(letters):
index = self.letters_to_neurons[(pos, letter)]
layer[index] = 1
return layer
def train(self, training_set, n_epochs=1, callback=None):
# build dataset
dataset = DataSet(self.n_input_neurons, self.n_output_neurons)
for (ltr,ph) in training_set:
for sample in self.generateSamples(ltr,ph):
dataset.addSample(*sample)
# build trainer
trainer = Trainer(self._pybrain_network, dataset, 0.01, 1.0, 0.9)
for i in xrange(n_epochs):
# run callback if present
if callback: callback()
# train network
error = trainer.train()
# record training errors
self.n_trainings = self.n_trainings + 1
self.training_errors.append(error)
def getInputHiddenWeights(self):
return self._in_hidden_connection.params.reshape((self.n_hidden_neurons, self.n_input_neurons))
def getHiddenOutputWeights(self):
return self._hidden_out_connection.params.reshape((self.n_output_neurons, self.n_hidden_neurons))
def getHiddenThresholds(self):
return self._bias_hidden_connection.params
def getOutputThresholds(self):
return self._bias_out_connection.params
def lettersToPhonemesWithAngles(self, letters, expected_phonemes):
for (window, exp_ph) in izip(self.windowIter(letters), expected_phonemes):
input_layer = self.letters_to_layer(window)
output_layer = self._pybrain_network.activate(input_layer)
phoneme = self.layer_to_phoneme(output_layer)
angle = _angle(output_layer, self.phoneme_to_layer(exp_ph))
yield (phoneme, angle)
def lettersToPhonemes(self, letters):
for window in self.windowIter(letters):
input_layer = self.letters_to_layer(window)
output_layer = self._pybrain_network.activate(input_layer)
phoneme = self.layer_to_phoneme(output_layer)
yield phoneme
def addRandomWeights(self, rand_fn):
cons = (self._in_hidden_connection, self._hidden_out_connection)
for c in cons:
for i in xrange(len(c.params)):
c.params[i] += rand_fn()