本文整理汇总了Python中layer.Layer类的典型用法代码示例。如果您正苦于以下问题:Python Layer类的具体用法?Python Layer怎么用?Python Layer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Layer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run
def run(self):
self.getMyIPMAC()
self.physicalClientSocket = socket(AF_INET, SOCK_STREAM)
self.physicalClientSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
self.physicalClientSocket.bind(Layer.PhysicalClient)
self.physicalClientSocket.listen(1)
while True:
self.msg.emit('Waiting datagram from Network layer')
self.package, success = Layer.receive(self.physicalClientSocket)
if success:
self.msg.emit('Received package from Network layer.')
destiny = json.loads(self.package)['destiny']
self.destiny = (destiny[0], destiny[1])
self.msg.emit ('Destiny = ' + str(self.destiny))
self.getDstMAC(self.destiny)
self.package = json.loads(self.package)['datagram']
if success:
if not self.mtuReceived:
self.connectAsClient(self.destiny)
self.createFrame_BinaryFile(self.package, 'binaryRequestClient.txt', 'blue')
if self.probCollision != 0:
while random.randint(0, 10) <= self.probCollision:
rand = random.randint(0, 10)
self.msg.emit('Collision detected, ' + str(rand) + ' seconds to retry...')
time.sleep(rand)
sent = Layer.send(self.destiny, 'binaryRequestClient.txt', self.myMTU)
self.msg.emit('Sent binary file to Physical server.')
if self.receiveFile(self.physicalClientSocket, 'binaryAnswer.txt'):
self.msg.emit('Received binary file from server')
self.answer = self.interpretPackage('binaryAnswer.txt', 'red')
sent = Layer.send(Layer.NetworkClient, self.answer)
示例2: receiveFromInternet
def receiveFromInternet(self):
try:
self.transportServerSocket = socket(AF_INET, SOCK_STREAM)
self.transportServerSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
self.transportServerSocket.bind(Layer.TransportServer)
self.transportServerSocket.listen(1)
print 'Listening...'
while True:
self.segment, success = Layer.receive(self.transportServerSocket)
self.transportProtocol = json.loads(self.segment)['transportProtocol']
self.msg.emit('Received a ' + self.transportProtocol + ' segment.')
if success:
if self.transportProtocol == 'TCP':
if not self.interpretTCPSegment(self.segment):
self.errorMsg.emit('Error trying to interpret TCP segment')
else: #UDP protocol
self.interpretUDPSegment(self.segment)
self.sendToApplication()
if self.receiveAnswer():
self.createSegment(self.transportProtocol, self.applicationPack)
sent = Layer.send(Layer.NetworkServer, self.segment)
print 'Answer sent to internet layer'
except KeyboardInterrupt:
print 'Shutting down transport server'
except Exception as exc:
exc_type, exc_obj, exc_tb = sys.exc_info()
error = exc_tb.tb_frame
line = exc_tb.tb_lineno
fileName = error.f_code.co_filename
print "Error! " + str(exc)
print 'error line = ' + str(line)
self.transportServerSocket.close()
示例3: get_layer
def get_layer(self, name):
try:
lyr = Layer(self, name)
lyr.fetch()
return lyr
except FailedRequestError:
return None
示例4: build
def build(self, X, y):
"""
Builds the neural network in tensorflow
"""
## First, create a placeholder for the targets
self.targets = tf.placeholder(tf.float32, shape=[None, self.n_categories])
## First, create the input layer
self.input_layer = Layer(n_neurons=self.n_features)
self.input_layer.build()
## Then create all the hidden layers
current_input_layer = self.input_layer
for layer in self.hidden_layers:
layer.build(current_input_layer)
current_input_layer = layer
## Create the output layer
self.output_layer = Layer(n_neurons=self.n_categories, activation=self.output_activation)
self.output_layer.build(current_input_layer)
## Define the cost function
self.cost = None
if self.cost_function == 'log-likelihood':
self.cost = tf.reduce_mean(-tf.log(1e-37 + tf.reduce_sum(self.targets * self.output_layer.output, reduction_indices=[1])))
else: ## cross-entropy
self.cost = tf.reduce_mean(-tf.reduce_sum(self.targets * tf.log(1e-37 + self.output_layer.output), reduction_indices=[1]))
## Define the regularization parameters and function
self.reg_lambda_param = tf.placeholder(tf.float32)
self.batch_size = tf.placeholder(tf.float32)
if self.regularization == 'l1':
self.reg_term = tf.reduce_sum(tf.abs(self.output_layer.weights))
for layer in self.hidden_layers:
self.reg_term += tf.reduce_sum(tf.abs(layer.weights))
elif self.regularization == 'l2':
self.reg_term = tf.reduce_sum(self.output_layer.weights * self.output_layer.weights)
for layer in self.hidden_layers:
self.reg_term += tf.reduce_sum(layer.weights * layer.weights)
else:
self.reg_term = None
## Add the regularization term to the cost function
if self.reg_term is None:
self.reg_cost = self.cost
else:
self.reg_cost = self.cost + (self.reg_lambda_param/(2*self.batch_size))*self.reg_term
## Define the train step
self.train_step = getattr(tf.train, '{0}Optimizer'.format(self.learning_algorithm))(self.learning_rate).minimize(self.reg_cost)
## Initialize everything
self.session.run(tf.initialize_all_variables())
示例5: run
def run(self):
while not self.exit:
self.clock.tick(GameConfig.FRAMES_PER_SECOND)
self.controls.poll_event()
self.draw_menu()
Layer.update()
return None
示例6: load
def load(path):
"""
Loads a neural network from a json file
@param (String) path - The path to load the neural network from
@returns (Network) - The neural network that was loaded
"""
network = Network()
try:
with open(path, "r+") as f:
network_data = "\n".join(f.readlines())
network_json = json.loads(network_data)
layers = network_json["layers"]
# For every layer in the network ...
for layer in layers:
neurons = []
# For every neuron in the layer ...
for neuron in layer["neurons"]:
weights = neuron["weights"]
bias = neuron["bias"]
activation = neuron["activation"]
# Choose the proper activation function and corresponding derivative
activation_func = None
derivative_func = None
if activation == Network.LINEAR:
activation_func = Network.ACTIVATION_LINEAR
derivative_func = Network.DERIVATIVE_LINEAR
elif activation == Network.SIGMOID:
activation_func = Network.ACTIVATION_SIGMOID
derivative_func = Network.DERIVATIVE_SIGMOID
elif activation == Network.TANH:
activation_func = Network.ACTIVATION_TANH
derivative_func = Network.DERIVATIVE_TANH
elif activation == Network.STEP:
activation_func = Network.ACTIVATION_STEP
derivative_func = Network.DERIVATIVE_STEP
# Create a neuron with the desired info
neuron = Neuron(0, activation_func, derivative_func)
neuron.weights = weights
neuron.bias = bias
# Add the processed neuron to the collection
neurons.append(neuron)
# Create a layer with the desired neurons
layer = Layer(0, 0, None, None)
layer.neurons = neurons
# Add the processed layer to the collection
network.layers.append(layer)
except:
raise Exception("Invalid Neural Network File @ {}!".format(path))
return network
示例7: run
def run(self):
try:
self.msg.emit('starting router')
self.physicalRouterSocket = socket(AF_INET, SOCK_STREAM)
self.physicalRouterSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
self.physicalRouterSocket.bind(Layer.PhysicalRouter)
self.host, self.port = Layer.PhysicalRouter
self.physicalRouterSocket.listen(1)
self.msg.emit("Setup:\nIP = " + str(self.host) + '\tPort = ' + str(self.port) + '\nListening...')
except Exception as exc:
self.errorMsg.emit('ERROR! It was not possible start the execution: \n' + str(exc))
try:
while True:
if not self.mtuSent:
self.getMyIPMAC()
self.connectAsServer(self.physicalRouterSocket)
#receive request file from client
if self.receiveFile(self.physicalRouterSocket, 'binaryRouterClientRequest.txt'):
self.package = self.interpretPackage('binaryRouterClientRequest.txt', 'blue')
#Sending to and receiving from network layer
Layer.send(Layer.NetworkRouter, self.package)
self.msg.emit('Waiting answer...')
self.answer, success = Layer.receive(self.physicalRouterSocket)
self.msg.emit('Received package from Network layer.')
serverIP = json.loads(self.answer)['destiny']
self.getDstMAC(serverIP[0])
self.answer = json.loads(self.answer)['datagram']
#Creating binary file of request from client and sending to server
self.createFrame_BinaryFile(self.answer, 'binaryRouterRequest.txt','green')
self.msg.emit ('Sending to server IP = ' + str(Layer.PhysicalServer))
self.connectAsClient(Layer.PhysicalServer)
self.msg.emit ('Request sent.');
success = Layer.send(Layer.PhysicalServer, 'binaryRouterRequest.txt', self.mtu)
#receiving answer from server
if success and self.receiveFile(self.physicalRouterSocket, 'binaryRouterServerAnswer.txt'):
self.package = self.interpretPackage('binaryRouterServerAnswer.txt', 'blue')
#sending to and receiving from network layer
Layer.send(Layer.NetworkRouter, self.package)
self.msg.emit('Waiting answer...')
self.answer, success = Layer.receive(self.physicalServerSocket)
self.msg.emit('Received package from Network layer.')
destiny = json.loads(self.answer)['destiny']
self.answer = json.loads(self.answer)['datagram']
#creating binary file of answer from server and sending to client
self.createFrame_BinaryFile(self.answer, 'binaryRouterAnswer.txt','green')
Layer.send(Layer.PhysicalClient, 'binaryRouterAnswer.txt', self.mtu)
except Exception as exc:
exc_type, exc_obj, exc_tb = sys.exc_info()
error = exc_tb.tb_frame
line = exc_tb.tb_lineno
fileName = error.f_code.co_filename
self.errorMsg.emit('ERROR! It was not possible run the server: \n' + str(exc)+
'\nLine = ' + str(line))
return False
示例8: __init__
def __init__(self,neuron_count_list = [1,1,1]):
self.layer_list = []
for i in xrange(1,len(neuron_count_list)):
#new layer with input count determined by the prev layer neuron count
new_layer = Layer(neuron_count_list[i-1])
for j in xrange(neuron_count_list[i]):
new_layer.add_neuron(weights=None,bias=None)
self.layer_list.append(new_layer)
示例9: load
def load(filename, input):
fptr = open(filename,'r')
data = json.load(fptr)
hidden_layer = []
for layer_data in data[:-1]:
layer = Layer.load(layer_data, input=input)
hidden_layer.append(layer)
input = layer.output
output_layer = Layer.load(data[-1], input=hidden_layer[-1].output)
fptr.close()
nnet = Network(new_network=False)
nnet.create_network(hidden_layer, output_layer)
return nnet
示例10: __init__
def __init__(self, data=None, projection=None, geotransform=None,
name=None, keywords=None, style_info=None):
"""Initialise object with either data or filename
NOTE: Doc strings in constructor are not harvested and exposed in
online documentation. Hence the details are specified in the
class docstring.
"""
# Invoke common layer constructor
Layer.__init__(self,
name=name,
projection=projection,
keywords=keywords,
style_info=style_info)
# Input checks
if data is None:
# Instantiate empty object
self.geotransform = None
self.rows = self.columns = 0
return
# Initialisation
if isinstance(data, basestring):
self.read_from_file(data)
elif isinstance(data, QgsRasterLayer):
self.read_from_qgis_native(data)
else:
# Assume that data is provided as a numpy array
# with extra keyword arguments supplying metadata
self.data = numpy.array(data, dtype='d', copy=False)
proj4 = self.get_projection(proj4=True)
if 'longlat' in proj4 and 'WGS84' in proj4:
# This is only implemented for geographic coordinates
# Omit check for projected coordinate systems
check_geotransform(geotransform)
self.geotransform = geotransform
self.rows = data.shape[0]
self.columns = data.shape[1]
self.number_of_bands = 1
# We assume internal numpy layers are using nan correctly
# FIXME (Ole): If read from file is refactored to load the data
# this should be taken care of there
self.nodata_value = numpy.nan
示例11: add_layer
def add_layer(self, layer=None, prev_layer=None, next_layer=None):
if len(self.layers) >= MAX_LAYERS_NUM:
return False
if (prev_layer is None) or (next_layer is None):
return False
if layer == None:
layer = Layer(next=next_layer)
else:
if (next_layer in layer.next_layer) or (prev_layer in layer.prev_layer):
print 'DA',
return False
self.layers.append(layer)
self.connect_layers(prev_layer, layer)
layer.modify(-1) # add random weight
return True
示例12: TrainingResultsTest
class TrainingResultsTest( unittest.TestCase ):
def setUp( self ):
self.tr = TrainingResults()
from opencl import OpenCL
from layer import InputLayer, Layer, OutputLayer, ExecutionContext
self.ocl = OpenCL( pyopencl.create_some_context() )
self.i = InputLayer( 2, self.ocl )
self.h = Layer( 3, self.ocl )
self.o = OutputLayer( 1, self.ocl )
self.i.link_next( self.h )
self.h.link_next( self.o, 0, 3 )
self.nnc = ExecutionContext( self.i, self.o, allow_training = True )
self.i.set_weights( numpy.array( [ 0.1 ] * self.i.weights_count, numpy.float32 ) )
self.h.set_weights( numpy.array( [ 0.2 ] * self.h.weights_count, numpy.float32 ) )
self.o.set_weights( numpy.array( [ 0.3 ] * self.o.weights_count, numpy.float32 ) )
def assertArrayEqual( self, ar1, ar2 ):
self.assertEqual( len( ar1 ), len( ar2 ) )
for x, y in zip( numpy.array( ar1, numpy.float32 ), numpy.array( ar2, numpy.float32 ) ):
self.assertAlmostEqual( x, y, places = 5 )
def test_store( self ):
self.tr.reset()
self.assertEqual( self.tr.iterations, numpy.int32( 0 ) )
self.assertGreater( self.tr.minimal_error, numpy.float32( 1e6 ) )
self.assertIsNone( self.tr.optimal_weights )
self.assertAlmostEqual( self.tr.total_time, 0.0 )
self.assertAlmostEqual( self.tr.opencl_time, 0.0 )
self.i.set_inputs( numpy.array( [1.0, 1.0], numpy.float32 ), is_blocking = True )
self.i.process()
initial_result = self.o.get_outputs()
self.tr.store_weights( self.nnc )
self.i.set_weights( numpy.array( [ 0.4 ] * self.i.weights_count, numpy.float32 ) )
self.i.process()
self.assertNotEqual( initial_result, self.o.get_outputs() )
self.tr.apply_weights( self.nnc )
self.i.process()
self.assertArrayEqual( initial_result , self.o.get_outputs() )
示例13: run
def run(self):
self.networkServerSocket = socket(AF_INET, SOCK_STREAM)
self.networkServerSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
self.networkServerSocket.bind (Layer.NetworkServer)
self.networkServerSocket.listen(1)
while True:
self.package, success = Layer.receive(self.networkServerSocket)
if success:
self.interpretPackage(self.package, 'blue')
sent = Layer.send(Layer.TransportServer, self.datagram['data'])
if sent:
self.answer, success = Layer.receive(self.networkServerSocket)
self.msg.emit ('Received answer')
networkPackage = self.createNetworkPackage(Layer.PhysicalClient,self.createDatagram(self.answer, 'red'))
sent = Layer.send(Layer.PhysicalServer, networkPackage)
self.msg.emit ('Answer sent to physical layer')
示例14: run
def run(self):
#TODO change ip
self.transportClientSocket = socket(AF_INET, SOCK_STREAM)
self.transportClientSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
self.transportClientSocket.bind(Layer.TransportClient)
#while True:
self.transportClientSocket.listen(1)
self.msg.emit ('Listening...')
try:
while True:
if self.receiveFromApplicationLayer():
if self.transportProtocol == 'TCP':
if not self.TCPConnected :
if self.threeWayHandshake():
self.msg.emit('TCP connection established successfully'\
' through TreeWayHandshake protocol')
else:
self.errorMsg.emit ('The ThreeWayHandshake protocol could not be completed.'\
'\nVerify you connection and try again.')
break;
else:
self.sendTCPPackage()
else: #transport protocol = UDP
if self.createSegment(self.transportProtocol, self.applicationPack):
sent = Layer.send(Layer.NetworkClient, self.segment)
if self.receiveAnswer():
#TODO recognize datagram
self.sendAnswerToApplicationLayer()
except KeyboardInterrupt:
self.transportClientSocket.close()
示例15: __init__
def __init__(self, input_num, output_num, lnum=3, delta=0):
super(NeuroNet, self).__init__()
self.input_layer = Layer(input_num)
self.output_layer = Layer(output_num)
self.layers = [Layer()]
self.connect_layers(self.input_layer, self.layers[0])
for i in xrange(lnum - 1):
self.layers.append(Layer())
self.connect_layers(self.layers[-2], self.layers[-1])
self.connect_layers(self.layers[-1], self.output_layer)
for i in self.layers:
i.modify(-1)
# for i in i.neurons:
# print i
self.set_input_delta(delta)