def test_set_config_attribute(self) : net = Network(self.empty()) net.p = "AudioSource" self.assertEquals(['1'], net.p._outports.__dir__() ) net.p.NSources = 2 self.assertEquals(['1', '2'], net.p._outports.__dir__() ) self.assertEquals(2, net.p.NSources)
def test_addProcessingWithName_types_AndFail(self): net = Network(self.empty()) try: net.types = "DummyPortSource" self.fail("Exception expected") except AssertionError, e: self.assertEquals("Wrong processing name: types is a method", e.__str__())
def getTotalPosts(self,url): xmldoc = Network().getURL(url) if xmldoc: try: xmldoc = minidom.parse(xmldoc) except ExpatError: logging.debug('unxexpected xml format') return False #log #return flase except OSError: logging.debug('unxexpected os error ') return False else: try: itemlist= xmldoc.getElementsByTagName('posts') return itemlist[0].attributes['total'].value except ExpatError: logging.debug("No posts or total, check url..") #log #return flase return False
def __init__(self, date=None): Network.__init__(self,date=date) if not date: self.date = '1970-01-01' edges = [(1, 2), (1, 3), (2, 4), (3, 2), (3, 5), (4, 2), (4, 5), (4, 6), (5, 6), (5, 7), (5, 8), (6, 8), (7, 1), (7, 5), (7, 8), (8, 6), (8, 7), ] for edge in edges: self.add_edge(edge[0], edge[1], 1.0)
def start_new(dimensions, learning_rate): n = Network(dimensions, learning_rate) current_accuracy = accuracy(n, test) best_accuracy = 0.0 best_net = copy.deepcopy(n) for j in range(800): if j % 2 == 0: new_accuracy = accuracy(n, test) print(str(j) + ": " + str(new_accuracy)) diff = abs(current_accuracy - new_accuracy) / (current_accuracy + new_accuracy) * 100 if diff < 0.01: n.learning_rate = l_rate * 2 else: n.learning_rate = l_rate if new_accuracy > best_accuracy: best_accuracy = new_accuracy best_net = copy.deepcopy(n) current_accuracy = new_accuracy for i in range(len(train)): n.cycle(train[i][:-1], convert_to_output_list(train[i][-1])) pickle.dump(best_net, open('best_net.p', 'wb'))
def scrapePage(self, url): tumblrPage=Network.getURL(url) if not (tumblrPage): pass try: xmldoc = minidom.parse(tumblrPage) postlist = xmldoc.getElementsByTagName('post') except ExpatError: logging.debug('unxexpected xml format') pass except AttributeError: logging.debug('unxexpected ATTRIBUTE ERROR') pass # log # return flase else: if((postlist or xmldoc)==False): pass for eachpost in postlist: #logging.debug(eachpost.attributes['type'].value) if (self.tagging): if not self.checkTags(eachpost): continue if eachpost.attributes['type'].value == 'photo': caption = self.checkCaption(eachpost) urls =eachpost.getElementsByTagName('photo-url') for eachurl in urls: imageUrl = self.getImageUrl(eachurl) if imageUrl: imageFile=Network.getURL(imageUrl) if imageFile: #logging.debug(imageFile.headers.items()) imageFilename=Parser.formatImageName(imageUrl) if(imageFilename): Parser.writeFile(imageFilename,imageFile) #logging.debug(eachpost.attributes.keys()) image={ 'name':imageFilename, 'caption':caption, 'account':eachpost.attributes['id'].value, 'url':eachpost.attributes['url'].value, 'id': eachpost.attributes['id'].value, 'source':eachpost.attributes['url-with-slug'].value } SQLLITE().insertImage(image) if eachpost.attributes['type'].value == 'video': videoUrl= self.getVideoUrl(eachpost) if videoUrl: videoFile=Network.getURL(videoUrl) if videoFile: videoFilename = Parser.formatVideoName(videoUrl) if videoFilename: Parser.writeFile(videoFilename,videoFile)
def test_code_whenNameIsAKeyword(self) : net = Network(self.empty()) net["while"] = "DummyPortSource" self.assertEquals( "network[\"while\"] = 'DummyPortSource'\n" "\n" "\n" , net.code())
def test_code_whenNameHasUnderlines(self) : net = Network(self.empty()) net.name_with_underlines = "DummyPortSource" self.assertEquals( "network.name_with_underlines = 'DummyPortSource'\n" "\n" "\n" , net.code())
def test_addProcessingAsItem(self) : net = Network(self.empty()) net["processing1"] = "DummyPortSource" self.assertEquals( "network.processing1 = 'DummyPortSource'\n" "\n" "\n" , net.code())
def test_set_config_attribute_with_hold_apply(self) : net = Network(self.empty()) net.p = "AudioSource" c = net.p._config c.hold() c.NSources = 2 self.assertEquals(1, net.p.NSources) c.apply() self.assertEquals(2, net.p.NSources)
def test_clone_and_apply(self) : net = Network(self.empty()) net.p = "AudioSource" c = net.p._config.clone() c.NSources = 2 self.assertEquals(1, net.p.NSources) self.assertEquals(2, c.NSources) net.p._config = c self.assertEquals(2, net.p.NSources)
def test_deleteProcessingAsItem(self): net = Network(self.empty()) net["processing1"] = "DummyPortSource" net["processing2"] = "DummyPortSink" del net["processing1"] self.assertEquals( "network.processing2 = 'DummyPortSink'\n" "\n" "\n" , net.code())
def test_deleteProcessingAsAttribute(self): net = Network(self.empty()) net.processing1 = "DummyPortSource" net.processing2 = "DummyPortSink" del net.processing1 self.assertEquals( "network.processing2 = 'DummyPortSink'\n" "\n" "\n" , net.code())
def gen_anomaly_dot(ano_list, netDesc, normalDesc, outputFileName): net = Network() net.init(netDesc, normalDesc) for ano_desc in ano_list: ano_type = ano_desc['anoType'].lower() AnoClass = ano_map[ano_type] A = AnoClass(ano_desc) net.InjectAnomaly( A ) net.write(outputFileName)
def test_code_for_changing_config_attributes(self): net = Network(self.empty()) net.proc1 = "DummyProcessingWithStringConfiguration" net.proc1.AString = 'newvalue' self.assertMultiLineEqual( "network.proc1 = 'DummyProcessingWithStringConfiguration'\n" "network.proc1['AString'] = 'newvalue'\n" "network.proc1['OtherString'] = 'Another default value'\n" "\n\n" , net.code(fullConfig=True))
def test_addTwoProcessingsDifferentType(self) : net = Network(self.empty()) net.processing1 = "DummyPortSource" net.processing2 = "DummyPortSink" self.assertEquals( "network.processing1 = 'DummyPortSource'\n" "network.processing2 = 'DummyPortSink'\n" "\n" "\n" , net.code())
def parseXml(node): field = Field() for child in node.childNodes: if child.tagName == Coordinates.getXmlName(): field.addBoundary(Coordinates.parseXml(child)) if child.tagName == Network.getXmlName(): field.addNetwork(Network.parseXml(child)) if child.tagName == District.getXmlName(): field.addDistrict(District.parseXml(child)) return field
def test_route_length(num_simulations, mixing_probability, drop_percentage): cumulative_length = 0 for i in range(num_simulations): manet = Network() manet.init() result, length = manet.run( drop_percentage=drop_percentage, mixing_probability=mixing_probability ) cumulative_length += length return cumulative_length / num_simulations
def test_codeShowsDescription(self): net = Network(self.empty()) net.description = "A description" net.proc1 = "DummyPortSink" self.assertEquals("A description", net.description) self.assertEquals( "network.description = 'A description'\n" "network.proc1 = 'DummyPortSink'\n" "\n" "\n" , net.code())
def test_codeForNumericConnectors(self): net = Network(self.empty()) net.proc1 = "ProcessingWithNumericPorts" net.proc2 = "ProcessingWithNumericPorts" net.proc1["2"] > net.proc2["1"] self.assertEquals( "network.proc1 = 'ProcessingWithNumericPorts'\n" "network.proc2 = 'ProcessingWithNumericPorts'\n" "\n" "network.proc1[\"2\"] > network.proc2[\"1\"]\n" , net.code())
def test_robustness(num_simulations, drop_percentage): success, failure = 0, 0 for i in range(num_simulations): manet = Network() manet.init() result = manet.run(drop_percentage=drop_percentage) if result: success += 1 else: failure += 1 return (success, failure, num_simulations)
def parseXml(node): district = District() district.districtType = int(node.getAttribute("type")) for child in node.childNodes: if child.tagName == Coordinates.getXmlName(): district.addBoundary(Coordinates.parseXml(child)) if child.tagName == Network.getXmlName(): district.addNetwork(Network.parseXml(child)) if child.tagName == Block.getXmlName(): district.addBlock(Block.parseXml(child)) return district
def test_addPortSourceAndPortSinkAndConnectPorts(self) : net = Network(self.empty()) net.processing1 = "DummyPortSource" net.processing2 = "DummyPortSink" net.processing1.OutPort1 > net.processing2.InPort1 self.assertEquals( "network.processing1 = 'DummyPortSource'\n" "network.processing2 = 'DummyPortSink'\n" "\n" "network.processing1.OutPort1 > network.processing2.InPort1\n" , net.code())
def main(): num_records = 10000 filename = 'Data/BKGDAT_Filtered.txt' n = Network(num_records, filename) flights = n.getDrillDown(orgs=['DXB'], dests=['DMM']) sorted_flights = n.f.getUniqueFlightsAndBookings(flights) for g, df in sorted_flights.groupby('BC'): print g, df brea
def test_is_public(self): net = Network(IPv4Address('192.168.255.128'), 25) self.assertFalse(net.is_public()) net = Network(IPv4Address('191.168.255.128'), 25) self.assertTrue(net.is_public()) net = Network(IPv4Address('127.168.255.128'), 25) self.assertFalse(net.is_public()) net = Network(IPv4Address('172.20.255.128'), 25) self.assertFalse(net.is_public()) net = Network(IPv4Address('10.20.255.128'), 25) self.assertFalse(net.is_public())
def test_changeNetworkVarForCode(self): net = Network(self.empty()) net.processing1 = "DummyControlSource" net.processing2 = "DummyControlSink" net.processing1.OutControl1 > net.processing2.InControl1 self.assertEquals( "net.processing1 = 'DummyControlSource'\n" "net.processing2 = 'DummyControlSink'\n" "\n" "\n" "net.processing1.OutControl1 > net.processing2.InControl1" , net.code("net"))
def login(self, login, password): if (login == '' or password == ''): return 1 try: #Obtention du verrou pour éviter les accès concurrents à la base de données self.lock.acquire() #On se connecte à la BDD conn = psycopg2.connect("dbname=puissance5 user=puissance5 password=Disco<3") #On récupère le hash de l'utilisateur cur = conn.cursor() cur.execute("SELECT password FROM users WHERE login=%s;", (login,)) #On vérifie qu'un utilisateur de ce nom existe if not cur.rowcount: self.lock.release() return 1 row = cur.fetchone() verify = pbkdf2_sha1.verify(password, row[0]) #On vérifie que le mot de passe est le bon if not verify: self.lock.release() return 1 cur.close() conn.close() self.lock.release() except Exception as e: self.lock.release() print("Echec de l'authentification du client : \n", e) return 4 #Création d'un code aléatoire de 20 caractères qui permettra au serveur de jeu d'identifier le client self.magic_code = generate_password(size=20) sb = StringBuilder() sb.add("4") sb.add(login) sb.add(str(self.magic_code)) game_server = Network(False) game_server.setHostAddress(self.game_server_address, self.game_server_port) game_server.connectToHost() #On envoie au serveur de jeu l'information que le client 'login' a le droit de se connecter en présentant le code magic_code game_server.send(sb.data.encode()) res = game_server.receive() response = StringExtract(res) if response[1] != '0': return 4 game_server.close() return 0
def test_addControlSourceAndControlSinkAndConnectControls(self) : net = Network(self.empty()) net.processing1 = "DummyControlSource" net.processing2 = "DummyControlSink" net.processing1.OutControl1 > net.processing2.InControl1 self.assertEquals( "network.processing1 = 'DummyControlSource'\n" "network.processing2 = 'DummyControlSink'\n" "\n" "\n" "network.processing1.OutControl1 > network.processing2.InControl1" , net.code())
def mitos12_cae_model_test(): model = { 'inputLayer' : ImageInputLayer(width=64,height=64,channels=3), 'hiddenLayers' : [ ConvolutionLayer(kernelsize=5, channels=3, features=12, stride=2, weightInitFunc=WeightInit.truncatedNormal, biasInitFunc=WeightInit.positive, activationFunc=tf.nn.relu, inputshape=[64,64,3]), ConvolutionLayer(kernelsize=5, channels=12, features=48, stride=2,weightInitFunc=WeightInit.truncatedNormal, biasInitFunc=WeightInit.positive, activationFunc=tf.nn.relu, inputshape=[32,32,12]), ConvolutionLayer(kernelsize=5, channels=48, features=192, stride=2,weightInitFunc=WeightInit.truncatedNormal, biasInitFunc=WeightInit.positive, activationFunc=tf.nn.relu, inputshape=[16,16,48]) ] } batch_size = 50 net = Network(Mitos12_CAE_Model, objective='reconstruction', batch_size=batch_size) net.setupTraining("squared-diff", "Adam") init = tf.initialize_all_variables() mitos12 = MITOS12Data(train_dirs=["/media/sf_VirtualDropbox"]) saver = tf.train.Saver() sess = tf.Session() with sess.as_default(): assert tf.get_default_session() is sess sess.run(init) saver.restore(sess, "/home/adrien/workspace/DeepNet/mitos12ConvAutoEncoder3.ckpt") xs = mitos12.next_batch(batch_size) rs = net.predict(xs) rs[0][rs[0]>1.] = 1. plt.figure(1) plt.subplot(2,3,1) plt.title('Original') plt.imshow(xs[0], interpolation='nearest') plt.axis('off') plt.subplot(2,3,2) plt.title('Reconstruction') plt.imshow(rs[0], interpolation='nearest') plt.axis('off') plt.gray() plt.subplot(2,3,4) plt.title('Diff - R') plt.imshow(np.abs(rs[0]-xs[0])[:,:,0], interpolation='nearest') plt.axis('off') plt.subplot(2,3,5) plt.title('Diff - G') plt.imshow(np.abs(rs[0]-xs[0])[:,:,1], interpolation='nearest') plt.axis('off') plt.subplot(2,3,6) plt.title('Diff - B') plt.imshow(np.abs(rs[0]-xs[0])[:,:,2], interpolation='nearest') plt.axis('off') plt.show()
def test_connect_sliceToPort(self): "CLAM limits inports connections" net = Network(self.empty()) net.proc1 = "Dummy6IOPorts" net.proc2 = "Dummy6IOPorts" net.proc1._outports[::2] > net.proc2.inport3 self.maxDiff = 1000 self.assertMultiLineEqual( "network.proc1 = 'Dummy6IOPorts'\n" "network.proc2 = 'Dummy6IOPorts'\n" "\n" "network.proc1.outport1 > network.proc2.inport3\n" , net.code())
class Bird(GameObject, GeneticClient): def __init__(self, xCoordinate, yCoordinate, objectWidth, objectHeight, imageBasePath, imageName): self.imageBasePath = imageBasePath self.imageName = imageName # initialize features to some value self.yDistanceNorthPipe = 50 self.yDistanceSouthPipe = Constants.GAP_BETWEEN_PIPES - self.yDistanceNorthPipe self.xDistancePipe = 100 self.isJump = False self.jump = Constants.JUMP_DISTANCE self.score = 0 self.health = Constants.GENETIC_LIFE self.alive = True self.lastPipeCrossed = None inputSize = 3 hiddenLayersSize = [10, 5] outputSize = 2 self.network = Network(inputSize, hiddenLayersSize, outputSize) super().__init__(xCoordinate, yCoordinate, objectWidth, objectHeight, imageBasePath, imageName) def copy(self): return Bird(self.xCoordinate, self.yCoordinate, self.objectWidth, self.objectHeight, self.imageBasePath, self.imageName) def recalculateFeatures(self, pipes): nearestPipeXDistance = 1000000 nearestPipePair = None for pipePair in pipes: northPipe = pipePair[0] # pipe has already passed the bird but it is in frame if (northPipe.xCoordinate + Constants.PIPE_WIDTH) < self.xCoordinate: continue # bird is in between the north pipe and south pipe if (self.xCoordinate + Constants.BIRD_WIDTH ) >= northPipe.xCoordinate & self.xCoordinate <= ( northPipe.xCoordinate + Constants.PIPE_WIDTH): nearestPipeXDistance = 0 nearestPipePair = pipePair break # update nearest pipe xDistance pipeDistance = northPipe.xCoordinate - self.xCoordinate if pipeDistance < nearestPipeXDistance: nearestPipePair = pipePair nearestPipeXDistance = pipeDistance self.xDistancePipe = nearestPipeXDistance self.yDistanceNorthPipe = nearestPipePair[ 0].yCoordinate + nearestPipePair[0].objectHeight - self.yCoordinate self.yDistanceSouthPipe = nearestPipePair[ 1].yCoordinate - self.yCoordinate def networkForwardStep(self): inputFeatures = np.array([ self.yDistanceNorthPipe, self.yDistanceSouthPipe, self.xDistancePipe ]) return self.network.forwardStep(inputFeatures) def changeCoordinate(self, changeBy): self.yCoordinate += changeBy def getFitness(self): return self.health def getNetwork(self): return self.network def saveObject(self, pickleFilePath): with open(pickleFilePath, 'wb') as f: pickle.dump(self, f) def display(self, window): displayYCoordinate = self.yCoordinate displayXCoordinate = self.xCoordinate if displayYCoordinate > Constants.BIRD_Y_COORDINATE_MAX: displayYCoordinate = Constants.BIRD_Y_COORDINATE_MAX if displayYCoordinate < Constants.BIRD_Y_COORDINATE_MIN: displayYCoordinate = Constants.BIRD_Y_COORDINATE_MIN coordinates = (displayXCoordinate, displayYCoordinate) loadedImage = pygame.image.load(self.imagePath) window.blit(loadedImage, coordinates)
def train(): # Load data (frames) from video - generate embedded frames of size: (img_sz x img_sz x 4) video_name = "movies/BG.mp4" data = DataProvider(video_name, img_sz) # STN - align the frames to the right position in the panorama # align_frames(data) # Learn the subspace using Autoencoder device = '/cpu:0' # '/gpu:0' OR '/cpu:0' with tf.device(device): # build the network net = Network(img_sz, d_sz) # calculate the number of batches per epoch # batch_per_ep = data.train_size // batch_size # print('Data size: ', data.train_size, ' Num of epochs: ', epoch_num, ' Batches per epoch: ', batch_per_ep) ae_inputs = tf.placeholder( tf.float32, (batch_size, img_sz, img_sz, 3)) # input to the network ae_outputs = net.simple1( ae_inputs) # fusion , simple1 , simple2, vae, fully_connected # calculate the loss and optimize the network loss = tf.reduce_mean( tf.square(ae_outputs - ae_inputs)) # claculate the mean square error loss train_op = tf.train.AdamOptimizer(learning_rate=lr).minimize(loss) # initialize the network init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) for ep in range(epoch_num): # epochs loop for i in range(iterations): # batches loop # read a batch -> batch_img batch_img = data.next_batch(batch_size, 'train') # print("batch size: ", batch_img.shape) _, c = sess.run([train_op, loss], feed_dict={ae_inputs: batch_img}) if not i % 10: print('Epoch {0}: Iteration: {1} Loss: {2:.5f}'.format( (ep + 1), i, c)) # test the trained network batch_img = data.next_batch(batch_size, 'train') #batch_img[4, ...] = generate_outliers(batch_img[4,...],50,80) # batch_img[7, ...] = generate_outliers(batch_img[7,...],40,110) test_results = sess.run([ae_outputs], feed_dict={ae_inputs: batch_img})[0] # plot the reconstructed images and their ground truths (inputs) imgs = [] imgs_test = [] titles = [] for i in range(10): imgs.append(batch_img[i, ...]) imgs_test.append(np.abs(test_results[i, ...])) titles.append('') fig1 = open_figure(1, 'Original Images', (7, 3)) PlotImages(1, 2, 5, 1, imgs, titles, 'gray', axis=True, colorbar=False) fig2 = open_figure(2, 'Test Results', (7, 3)) PlotImages(2, 2, 5, 1, imgs_test, titles, 'gray', axis=True, colorbar=False) plt.show() fig1.savefig('f1.png') fig2.savefig('f2.png')
for prop in proportions_opts: weighted_F1_array = [] test_acc_array = [] for iter in range(iterations): ws = 100 accumulation_steps = 5 epochs = 30 batch_size = 100 learning_rate = 0.00001 #df = pd.read_csv('/data/sawasthi/Thesis--Create-Synthetic-IMU-data/MoCAP/norm_values.csv') #df = pd.read_csv('S:/MS A&R/4th Sem/Thesis/Github/Thesis- Create Synthetic IMU data/MoCAP/norm_values.csv') #value = df.values.tolist() #print(len(df),len(value), len(value[0])) Lara_net = Network(config) Lara_net.init_weights() normal = torch.distributions.Normal(torch.tensor([0.0]), torch.tensor([0.001])) #noise = noise.float() criterion = nn.CrossEntropyLoss() #model_path = '/data/sawasthi/data/JHMDB/model/model_tl.pth' #model_path = 'S:/MS A&R/4th Sem/Thesis/LaRa/OMoCap data/model.pth' #model_path = 'S:/MS A&R/4th Sem/Thesis/PAMAP2_Dataset/' #model = torch.load(model_path) # transformed_net model = load_weights(Lara_net) model = model.to(device) print("model loaded") if config["freeze"]:
def __init__(self): self.networks = [None] * params.population_size for i in range(params.population_size): self.networks[i] = Network()
from Training import Training from Network import Network import torch network1 = Network(49, 128) dir1 = r"C:\Users\Abgedrehter Alman\PycharmProjects\bot\models and trajectory\model3" training = Training(dir1) training.train()
def construct_network(n_features, n_classes, n_hidden_layers, size_hidden_layers): numpy.random.seed(1) size_network = [n_features] + size_hidden_layers + [n_classes] return Network(size_network)
class HumanVSAIForServer: def __init__(self, model_file=None, human_name=None): self.human_file = human_name + '.pickle' self.ai_file = human_name + '_ai.pickle' ai_board, human_board = self.resumeBoards() self.network = Network(BOARD_WIDTH, BOARD_HEIGHT, len(SHIPS)) if not model_file is None: self.network.restoreModel('./models/mymodel') self.ai_game = Game(BOARD_WIDTH, BOARD_HEIGHT, SHIPS, board=ai_board, network=self.network) self.human_game = Game(BOARD_WIDTH, BOARD_HEIGHT, SHIPS, board=human_board) self.cur_step = 0 def getBothBoardsString(self): ai_board_printer = self.ai_game.board.getViewState() human_board_printer = self.human_game.board.getViewState() spaces = ' ' output_board = [] for i in range(len(ai_board_printer)): output_board.append(ai_board_printer[i] + spaces + human_board_printer[i]) # for i in range(len(output_board)): # print(output_board[i]) output_string = ' AI YOU \n' for i in range(len(output_board)): output_string += output_board[i] + '\n' return output_string def getGameStateString(self): state_number = self.ai_game.board.state_number ai_hits = 0 for i in range(len(state_number)): for j in range(len(state_number[0])): if state_number[i][j] == 1: ai_hits += 1 human_board_printer = self.human_game.board.getViewState() output_board = ['AI hit: ' + str(ai_hits)] for i in range(len(human_board_printer)): output_board.append(human_board_printer[i]) output_board = 'AI hit: ' + str(ai_hits) + '\n' for i in range(len(human_board_printer)): output_board += human_board_printer[i] + '\n' return output_board def takeOneMove(self, human_move): human_next_move = self.getHumanMoveInput(human_move) if human_next_move is None: return ( None, self.getGameStateString() + 'Invalid Input, please switch to English input and input the following format: row,column, e.g. 2,4\n Or you can input "reset" to reset the game' ) ai_input_dimensions = self.ai_game.board.getInputDimensions() human_input_dimensions = self.human_game.board.getInputDimensions() ai_available_moves = self.ai_game.board.getNextAvailableBombLocations() ai_next_move = self.ai_game.getBestMoveBasedOnModel( ai_input_dimensions, ai_available_moves) # print(ai_next_move) (ai_input_dimensions, _, _) = self.ai_game.takeAMove(ai_next_move) (human_input_dimensions, _, _) = self.human_game.takeAMove(human_next_move) self.cur_step += 1 is_ai_win = self.ai_game.board.checkIfGameFinished() is_human_win = self.human_game.board.checkIfGameFinished() if is_ai_win: self.deleteFiles() return (None, self.getBothBoardsString() + 'LOL AI wins !') if is_human_win: self.deleteFiles() return (None, self.getBothBoardsString() + 'You win !') # Save the game self.saveBoards() return (1, self.getGameStateString()) def getHumanMoveInput(self, human_move): if human_move == '': return None xy = human_move.split(',') if len(xy) != 2: return None x = int(xy[0]) y = int(xy[1]) if x >= self.human_game.board.board_height or x < 0: return None if y >= self.human_game.board.board_width or y < 0: return None location = x * self.human_game.board.board_width + y if self.human_game.board.available_bomb_locations[location] != 1: return None return location def saveBoards(self): with open(self.ai_file, 'wb') as ai_f: pickle.dump(self.ai_game.board, ai_f, pickle.HIGHEST_PROTOCOL) with open(self.human_file, 'wb') as human_f: pickle.dump(self.human_game.board, human_f, pickle.HIGHEST_PROTOCOL) # Resume game, or set up a new game if not found def resumeBoards(self): ai_board = None human_board = None if not os.path.isfile(self.human_file) or not os.path.isfile( self.ai_file): return (ai_board, human_board) with open(self.ai_file, 'rb') as ai_f: ai_board = pickle.load(ai_f) with open(self.human_file, 'rb') as human_f: human_board = pickle.load(human_f) return (ai_board, human_board) def deleteFiles(self): os.remove(self.human_file) os.remove(self.ai_file)
correct = 0 total_loss = 0.0 total_correct = 0 epochs = 80 batch_size = 200 lr_factor = 1 l = [] tot_loss = 0 accuracy = [] learning_rate = 0.00001 print("accumulation_steps ", accumulation_steps, "batch_size", batch_size, "epochs", epochs, "accumulation_steps ", accumulation_steps,"sliding_window_length", config["sliding_window_length"]) #df = pd.read_csv('/data/sawasthi/Thesis--Create-Synthetic-IMU-data/MoCAP/norm_values.csv') #df = pd.read_csv('S:/MS A&R/4th Sem/Thesis/Github/Thesis- Create Synthetic IMU data/MoCAP/norm_values.csv') #value = df.values.tolist() #print(len(df),len(value), len(value[0])) model = Network(config) model = model.float() model = model.to(device) #model.load_state_dict(torch.load()) #print("model loaded") # normal = torch.distributions.Normal(torch.tensor([0.0]),torch.tensor([0.001])) #noise = noise.float() criterion = nn.CrossEntropyLoss() #optimizer = optim.Adam(model.parameters(), lr=0.001) optimizer = optim.RMSprop(model.parameters(), lr=learning_rate, alpha=0.9,weight_decay=0.0005, momentum=0.9) #optimizer = optim.SGD(model.parameters(), lr=0.0001, momentum=0.9) model_path = '/data/sawasthi/Penn/model/model_acc_cnn_down2.pth' #model_path = 'S:/Datasets/Penn_Action/Penn_Action/model_test.pth' #model_path = 'S:/MS A&R/4th Sem/Thesis/PAMAP2_Dataset/'
import load_data from Convolutional_Network import CNNetwork from Network import Network import numpy as np training_data, patch_data, validation_data, test_data = load_data.load_data_wrapper( ) netff0 = Network([784, 100, 25, 10]) netff0.SGD(training_data[:10000], 10, 10, .5, test_data=test_data[:100]) netcff0 = CNNetwork([100, 25, 10], patch_data, n_clusters=16, patch_size=(8, 8), pool_size=(5, 5)) netcff0.SGD(training_data[:10000], 10, 10, .5, test_data=test_data[:100], convolve=True) np.random.seed(seed=0) altered_training_data = [(load_data.random_maniputlate_image(img), key) for img, key in training_data[:10000]] altered_test_data = [(load_data.random_maniputlate_image(img), key) for img, key in test_data[:100]] netcff1 = CNNetwork([100, 25, 10], patch_data, n_clusters=16,
def startGame(self): n = Network() self.teamID = int(n.getData()) playerString = "TEAMID: " + str(self.teamID) self.guiClient = self.gui.render(playerString, False, (225, 225, 250)) interface = Interface.Interface(self.screen, self.teamID, self.gui) self.inProc = InputProcessor.InputProcessor(self.teamID, interface.globalActions) self.resources = {'$': 50, 'ß': 0, '¶': 8} self.makeResText() buildingSpacing = (self.screenHeight * 2 / 3) / 8 for b in range(7): xMod = -0.00065 * ( (b * buildingSpacing) - self.screenHeight / 3)**2.0 + 96 self.buildingList[0].append( Buildings.EmptySpace(0, xMod, b * buildingSpacing + 72, self.gui)) self.buildingList[1].append( Buildings.EmptySpace(1, self.screenWidth - xMod, b * buildingSpacing + 72, self.gui)) # dictionary formatting: # { unitID : [teamID, x, y, health, imageIndex, direction, targetX, currentState, healthMod, unitType] } # currentState: 0 is idle, 1 is moving, 2 is attacking depthSortTimer = 8 moneyTimer = 60 peopleTimer = 600 while self.started: prevResources = self.resources.copy() for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_t: ssName = "screenshot.png" pygame.image.save(self.screen, ssName) if event.key == pygame.K_ESCAPE: sys.exit() # draw the background self.drawBG(self.screen) # per server communication or whatever: # signature = self.teamID # sent buildings list setup self.buildingsToSend = [ b.sprID for b in self.buildingList[self.teamID] ] # sent unit making list setup unitsToSend = [ b.queuedUnit for b in self.buildingList[self.teamID] if b.queuedUnit ] # reset queued units for b in self.buildingList[self.teamID]: b.queuedUnit = None selections = self.inProc.updateSelection(self.unitList, self.screen) self.inProc.updateBuildingSelection(self.buildingList, pygame.mouse) self.inProc.checkButtonsClicked(pygame.mouse, self.resources) states = -1 for i in range(len(interface.globalActions)): b = interface.globalActions[i] if b.clicked: states = i self.inProc.drawSelection(self.screen, self.unitList) # print(selections) keys = pygame.key.get_pressed() # print([(i, v) for i, v in enumerate(keys) if v == 1] toSend = { "keys": keys, "selections": selections, "buildings": self.buildingsToSend, "units": unitsToSend, "moveState": states } n.send(self.jEncode(toSend)) # ^ SEND STUFF v PROCESS RECEIVED STUFF self.receivedData = self.jDecode(n.getData()) # print([(uID, params[3], params[4], params[7]) for uID, params in self.receivedData.items()]) for unitID, paramList in self.receivedData['units'].items(): unitID = int(unitID) found = False u = None for un in self.unitList: if un.unitID == unitID: found = True u = un break if not found: u = self.spawnUnit(unitID, paramList[9], paramList[0], paramList[1], paramList[2], paramList[8]) # make spawning effect self.fxList.append( Effects.SpawnEffect(paramList[1], paramList[2])) u.x = paramList[1] u.y = paramList[2] u.health = paramList[3] u.imageIndex = paramList[4] u.direction = paramList[5] u.updateSprites(paramList[6]) if paramList[7] == 0: u.sprite = u.spriteIdle elif paramList[7] == 1: u.sprite = u.spriteMoving elif paramList[7] == 2: u.sprite = u.spriteAttack if u.health <= 0: self.unitList.remove(u) for u in self.unitList: if str(u.unitID) not in self.receivedData['units']: u.selected = False self.unitList.remove(u) # this updates enemy building sprites based on their upgrades self.updateBuildings() # money gain timers here if moneyTimer > 0: moneyTimer -= 1 else: moneyTimer = 60 self.resources['$'] += 1 self.makeResText() if peopleTimer > 0: peopleTimer -= 1 else: peopleTimer = 600 self.resources['¶'] += 1 self.makeResText() if depthSortTimer > 0: depthSortTimer -= 1 else: depthSortTimer = 8 self.unitList.sort(key=lambda x: x.y * -1, reverse=True) for bL in self.buildingList: for b in bL: b.action(self.screen) i = 0 for b in self.buildingList[self.teamID]: # 'harvests' resources generated by buildings if b.generatedResources[1] > 0: self.resources[ b.generatedResources[0]] += b.generatedResources[1] b.generatedResources[1] = 0 if b.markedForDelete: self.buildingList[self.teamID][i] = b.upgradeBuilding if self.inProc.selectedBuilding == b: print("updating selection to new building") b.selected = False self.buildingList[self.teamID][i].selected = True self.inProc.selectedBuilding = self.buildingList[ self.teamID][i] i += 1 for u in self.unitList: u.draw(self.screen) for f in self.fxList: f.draw(self.screen) for f in self.fxList: if f.markedForDelete: self.fxList.remove(f) del f # updates rendered resources text of the player self.updateResText(prevResources) # self.inProc.draw(self.screen) self.screen.blit(self.guiClient, (22, 22)) interface.draw(self.screen) self.inProc.drawSelectionInterface(self.screen) # draw the resources of the player ((somewhere)) self.screen.blit(self.resourceGui, (self.screenWidth - 280, self.screenHeight - 24)) self.clock.tick(60) pygame.event.pump() if self.screenScalingEnabled: self.scaledScreen.blit(pygame.transform.scale2x(self.screen), (0, 0)) else: self.scaledScreen.blit(self.screen, (0, 0)) pygame.display.update()
class NeuronTesting(unittest.TestCase): """ """ def setUp(self) -> None: self.labels = [[1, 0]] self.layers = { 'layer 1': { 'activation': 'sigmoid', 'neurons': 2, }, 'layer 2': { 'activation': 'sigmoid', 'neurons': 2, } } self.data_set = [[0.2, 0.41, 0.42, 0.11, 0.52]] self.network = Network(len(self.data_set[0]), self.layers) self.neuron_inputs = [[0.23, 0.51, 0.24, 0.12], [], [1, -5, -200000, 2]] self.neuron_activations = ['sigmoid', 'sigmoid', 'relu'] self.test_neurons = [] for neuron_input, neuron_activation in zip(self.neuron_inputs, self.neuron_activations): self.add_neuron(neuron_input, neuron_activation) def add_neuron(self, neuron_input, neuron_activation): self.test_neurons.append(Neuron(len(neuron_input), neuron_activation)) def test_sigmoid(self): print("\nSigmoid Test:") self.assertAlmostEqual(sigmoid(1), 0.731058, 5) self.assertAlmostEqual(sigmoid(1, True), 0.1966119, 5) def test_relu(self): print("\nRelu Test:") self.assertAlmostEqual(relu(1), 1) self.assertAlmostEqual(relu(0, True), 1) def test_neuron_init(self): print("\nNeuron init Test:") with self.assertRaises(ValueError): self.add_neuron([0], 'other') def test_weights(self): print("\nWeight init Test:") output_layer = self.network.layers[-1] for neuron in output_layer: print( f"\tNeuron Weights: {[np.around(i, 3) for i in neuron.weights]}" ) self.assertIsNotNone(neuron.weights) def test_neuron_run(self): print("\nRun Neuron Test:") for test_neuron, neuron_input in zip(self.test_neurons, self.neuron_inputs): test_neuron.run(neuron_input) print(f"Neuron output: {test_neuron.output}") print(f"Neuron type: {test_neuron.activation_function.__name__}") self.assertNotEqual(test_neuron.output, 0.0) def test_output_backprop(self): print("\nOutput Backprop Test:") output_layer = self.network.layers[-1] self.network._gen_output_errors([1, 0]) self.assertEqual(output_layer[0].error_gradient, 1) def test_hidden_backprop(self): print("\nHidden Backprop Test:") hidden_layers = self.network.layers[0:-1] self.network._gen_output_errors(self.labels[0]) self.network._gen_hidden_errors() hidden_errors = [] for layer in hidden_layers: hidden_errors.append([neuron.error_gradient for neuron in layer]) self.assertEqual(len(hidden_errors[0]), 2) def test_hidden_backprop_changes(self): print("\nHidden Backprop Changes Test:") self.network._gen_output_errors(self.labels[0]) self.network._gen_hidden_errors() hidden_layers = self.network.layers[0:-1] previous_errors = [] for layer in hidden_layers: previous_errors.append([neuron.error_gradient for neuron in layer]) self.network.update_weights(self.data_set[0]) self.network._gen_output_errors(self.labels[0]) self.network._gen_hidden_errors() current_errors = [] for layer in hidden_layers: current_errors.append([neuron.error_gradient for neuron in layer]) self.assertNotEqual(current_errors, previous_errors) def test_update_input_weights(self): input_layer = self.network.layers[0] self.network._gen_output_errors([1, 0]) self.network._gen_hidden_errors() previous_weights = [] for neuron in input_layer: previous_weights.append( [weight.copy() for weight in neuron.weights]) self.network._update_input_weights(self.data_set[0]) current_weights = [] for neuron in input_layer: current_weights.append( [weight.copy() for weight in neuron.weights]) self.assertNotEqual(previous_weights, current_weights) def test_update_hidden_weights(self): hidden_weights = self.network.layers[1:] self.network.forward_prop(self.data_set[0]) self.network._gen_output_errors([1, 0]) self.network._gen_hidden_errors() previous_weights = [] for layer in hidden_weights: for neuron in layer: previous_weights.append( [weight.copy() for weight in neuron.weights]) self.network._update_hidden_weights() current_weights = [] for layer in hidden_weights: for neuron in layer: current_weights.append( [weight.copy() for weight in neuron.weights]) print(f'First Sample: {previous_weights[0][0]}') print(f'Second Sample: {current_weights[0][0]}') self.assertNotEqual(previous_weights, current_weights)
if option.is_gd(): print("\tOptimization: Gradient Descent") elif option.is_sgd(): print("\tOptimization: Stochastic Gradient Descent") if option.is_linear(): print("\tActivation Function: Linear") elif option.is_sigmoid(): print("\tActivation Function: Sigmoid") if option.is_l2norm(): print("\tRegularization: L2Norm") elif option.is_dropout(): print("\tRegularization: Drop out") net = Network(training, test, option) if read_weights_from_file == "y": loaded = np.load('weights.npz') net.set_hid_weights(loaded['hid_weights']).set_out_weights( loaded['out_weights']).set_hid_bias( loaded['hid_bias']).set_out_bias(loaded['out_bias']) plotter = LossAccPlotter(show_acc_plot=False) for i in range(NUM_OF_ITER): training_loss = 0 if read_weights_from_file != "y": training_loss = net.train(i == NUM_OF_ITER - 1) test_loss = net.test(i == NUM_OF_ITER - 1) plotter.add_values(i, loss_train=training_loss, loss_val=test_loss) plotter.block()
from Network import Network import networkx as nx import copy nw = Network() nw.read_neuron_data() # Create network with all nodes/edges read in from C. Elegans connectivity data starting_graph = nw.graph few_sensors_graph = copy.deepcopy(starting_graph) #nodes = [node for node in nw.nodes if not nw.neurons[node].is_muscle() and node != "PVDR" and node != "VC06"] muscles = [node for node in nw.nodes if nw.neurons[node].is_muscle()] sensors = [node for node in nw.nodes if nw.neurons[node].is_sensor()] oldsensors = copy.deepcopy(sensors) oldsensors.remove("ALML") oldsensors.remove("ALMR") oldsensors.remove("AVM") nonsensors = [node for node in nw.nodes if not nw.neurons[node].is_sensor()] sensors = ["ALML", "ALMR", "AVM"] # Sensors selected for the paper which yield z = 89 few_sensors_graph.remove_nodes_from(oldsensors) few_sensors_graph.remove_nodes_from( ["MVULVA", "MANAL"] ) # few_sensors_graph now = no sensors except ALML/R, AVM, also remove MVULVA, MANAL (vulva and anal muscles); w/o this change lower bound = 91 base_graph = copy.deepcopy(few_sensors_graph) base_graph.remove_nodes_from(muscles) base_graph.remove_nodes_from(["PVDR", "VC06"]) # Excluded as in paper """(in addition to the neurons in the pharyngeal system, CANL/R and VC06, which do not make connections with the rest of
class DNA2048: def __init__(self, genome=[]): '''Elemento DNA2048 con genoma aleatorio (o no, si se pasa como argumento) El genoma es una red neuronal ''' self.genome = genome if (not genome): self.genome = Network(N_INPUTS, N_OUTPUTS) def calcFitness(self): '''Calcula el fitness del elemento en base a la puntuación obtenida en un tablero aleatorio ''' self.juego = Juego2048() while (self.juego.libres() > 0): #Efectúa un movimiento hasta que pierda self.juego.add() tablero = self.juego.getTablero() inputs = [] #Tablero convertido a array unidimensional outputs = [] #Salida de la red neuronal for fila in tablero: for casilla in fila: #inputs.append(casilla) inputs.append( (log(casilla, 2) if (casilla != 0) else 0) / self.juego.getMax()) outputs = self.genome.getOutput(inputs) #Selección de movimiento a partir de outputs while (tablero == self.juego.getTablero() and any(outputs)): output = 0 for o in range(N_OUTPUTS): if abs(outputs[o]) > abs(outputs[output]): output = o if output == 0: self.juego.mover("w") elif output == 1: self.juego.mover("s") elif output == 2: self.juego.mover("a") elif output == 3: self.juego.mover("d") outputs[output] = 0 self.fitness = self.juego.getScore() #self.fitness /= 4096.0 def crossover(self, partner): '''Devuelve un genoma producto de combinar aleatoriamente self y partner ''' self.genome.crossover(partner.getGenome()) def mutate(self, prob): '''La red muta ''' self.genome.mutate(prob) def clone(self): '''Devuelve un elemento 2048 con mismo genoma que self ''' return DNA2048(self.genome) def show(self): self.juego.imprimir() print "Score:\t\t" + str(self.fitness) def getFitness(self): return self.fitness def getGenome(self): return self.genome
dropout=0.0) network_second = create_core(batchsize=batchsize, window=window, dropout=0.0, depooler=lambda x, **kw: x / 2) #print('network_second[1]: ',network_second[0]) #print('network_second params: ',network_second.params) #network_second.params=[W,b,W,b] #print('after network_second') network_second.load(np.load('network_core.npz')) #print('after network_second load') #print('network_second params: ',network_second.params) #network_second.params=[W,b,W,b] network = Network(network_first, network_second[1], params=network_first.params) #print('after creating a network made of previous networks') #print('network params: ',network.params) network.load(np.load('network_regression_kick.npz')) #print('after loading network_regression_kick') #print('network type: ',type(network)) #test1 = np.load('network_core.npz') #print('network_core.npz: ', test1.files) #test = np.load('network_regression_kick_sam.npz') #print('network_regression_kick.npz: ', test.files) #network_core.npz: ', ['L001_L002_W', 'L000_L001_W', 'L001_L003_b', 'L000_L002_b']) #('network_regression_kick.npz: ', ['L000_L006_b', 'L000_L002_b', 'L000_L005_W', #'L000_L009_W', 'L001_L002_W', 'L000_L010_b', 'L000_L001_W', 'L001_L003_b'] from AnimationPlot import animation_plot
from Network import Network n = Network(N=30,N_types=3,threshold=.6,empty_percent=.1) '''n.plotState() n.moveStep() n.plotState()''' #,init_config=rand n.drawGrid(gen=40)
from Dijkstra import Dijkstra from Network import Network from Presenter import Presenter from View import View network = Network("DistanzenNeu.csv") dijkstra = Dijkstra(network) view = View() presenter = Presenter(view, dijkstra)
import numpy as np import idx2numpy import matplotlib.pyplot as plt from Network import Network epochs = 30 network_architecture = [784, 20, 10] train_images_file = 'train-images.idx3-ubyte' train_images_array = idx2numpy.convert_from_file(train_images_file) input_matrix = np.reshape(train_images_array, (60000, 28 * 28)) / 500 train_labels_file = 'train-labels.idx1-ubyte' train_labels_array = idx2numpy.convert_from_file(train_labels_file) test_images_file = 't10k-images.idx3-ubyte' test_images_array = idx2numpy.convert_from_file(test_images_file) input_matrix_test = np.reshape(test_images_array, (10000, 28 * 28)) input_matrix_test = np.reshape(test_images_array, (10000, 28 * 28)) / 500 test_labels_file = 't10k-labels.idx1-ubyte' test_labels_array = idx2numpy.convert_from_file(test_labels_file) network1 = Network(network_architecture) network1.SGD(input_matrix, train_labels_array, epochs, input_matrix_test, test_labels_array)
import tensorflow as tf import numpy as np import os.path as osp import sys import gc from Network import Network from config import cfg from Imdb import Imdb from ObjectDetector import ObjectDetector if __name__ == '__main__': """ tests a pretrained faster rcnn model on the coco test2017 dataset """ sess = tf.Session(graph=tf.Graph()) net = Network(cfg, False, sess) imdb = Imdb('/content/image_data', 'test2017', cfg) od = ObjectDetector(net, imdb, cfg) od.evaluate( '/content/gdrive/My Drive/Faster-R-CNN/results/coco_results.json', True)
from Network import Network from BoardTools import BoardTools import numpy as np sizes = np.array([3,2,3]) network = Network(sizes) print(network.processVector([1,1,1]))
pipeList.append( Pipe(value["pipe_id"], key, value["length"], value["inter_diameter"], value["wall_thickness"], pipeMaterial, pipeInsulationLayer, pipeAntiCorrosionLayer, value["pipe_unit_construction_price"])) elif value["class_name"] == "Source": sourceList.append( HeatSource(value["source_id"], key, value["mass_flow_rate"], value["energy_supply"], value["energy_source_type"], value["heating_device_price"], value["heating_device_installation_price"], value["heating_period_per_year"])) elif value["class_name"] == "Load": loadList.append( HeatLoad(value["load_id"], key, value["heating_area"])) heatNetwork = Network(pipeList, loadList, sourceList, valveList, pumpList) salesInfo = SalesInfo(heatNetwork, fileText["SalesInfo"]["heating_unit_price"]) loanInfo = LoanInfo(fileText["LoanInfo"]["loan_raise_rate"], fileText["LoanInfo"]["lending_rate"], fileText["LoanInfo"]["loan_period"], fileText["LoanInfo"]["interest_compounded_number"]) employee = Employee( fileText["ManagementInfo"]["employees"]["number"], fileText["ManagementInfo"]["employees"]["average_salary"]) managementInfo = ManagementInfo( heatNetwork, fileText["ManagementInfo"]["energy_source_unit_price"], fileText["ManagementInfo"]["water_unit_price"], employee,
def __init__(self, model_file, json_file, model_cardinalities): self.model = Network() self.model.load_state_dict(torch.load(model_file)) self.experiment_gen = TwinDataGenerator(model_cardinalities) self.output = self.gen_all_cond_probs()
from Network import Network from helpers import helpers import sys topologyData = [] fileName = str(sys.argv[1]) rounds = int(sys.argv[2]) topologyData = helpers.readFile(fileName) sim = Network(sys.argv[1], topologyData) sim.createNodes() sim.beginRounds(rounds) sim.printResult() # all tables coverged with open('outputfile.txt', 'a') as outFile: outFile.write('\n\tPOST CONVERGENCE\t\n') outFile.write(sim.route) outFile.write(sim.returnRoutes())
def __init__(self): Network.__init__(self) self.filters = 250 self.kernel_size = 3
with open('ConditionalProbabilities.json', 'w') as outfile: json.dump(json_arr, outfile) return result ''' def gen_all_cond_probs(self): result = [] all_tensors = self.experiment_gen.get_all_options() print(len(all_tensors)) for option in all_tensors: input = TwinDataGenerator.get_tensor_from_dict(option) output = self.model.forward(input) for i in range(len(output)): current = "P[(Ouput is " + str(i) + ") | (inputs are: " + str(option) + ")]" result.append(current) return result bf = BiasFinder("Model.pt", "CreditScoreData.json", Network.get_cardinalities()) result = bf.gen_all_cond_probs() with open("AllCondProbs.json", 'w') as outfile: json.dump(result, outfile)
a_size = 4 # Agent can move Left, Right, or Fire load_model = False model_path = './model' game_env = 'Breakout-v0' tf.reset_default_graph() with tf.device("/cpu:0"): global_episodes = tf.Variable(0, dtype=tf.int32, name='global_episodes', trainable=False) trainer = tf.train.RMSPropOptimizer(learning_rate=7e-4, epsilon=0.1, decay=0.99) master_network = Network(height, width, depth, s_size, a_size, 'global', None) # Generate global network num_workers = 1 # Set workers ot number of available CPU threads print 'Creating', num_workers, 'workers' workers = [] # Create worker classes for i in range(num_workers): workers.append( Worker(game_env, i, (height, width, depth, s_size), a_size, trainer, model_path, global_episodes)) saver = tf.train.Saver(max_to_keep=5) with tf.Session() as sess: coord = tf.train.Coordinator() print('Loading Model...')
traffics = pickle.load(f) # Limit early trigger to N steps before N = 2 for t in traffics: t.transition = {(i[0], k): set(j[0] for j in J) for (i, k), J in t.transition.items() if i[0] - k <= N} # Create TA models from traffic models ETC_CL1 = ETCTimeTA(traffics[0], clock_name='c') ETC_CL2 = ETCTimeTA(traffics[1], clock_name='c') loops = [] loops.append(ControlLoop(ETC_CL1, f'cl1')) loops.append(ControlLoop(ETC_CL2, f'cl2')) net = Network(1, 5) loops.append(net) for loop in loops: loop.template.layout(auto_nails=True) # Synchronizing actions need to be defined here sync = ["up", "down", "timeout", "ack", "nack"] ntga = NTA(*loops, synchronisation=sync) ntga.template.declaration = f'{ntga.template.declaration}\nint EarNum;\nint EarMax = 4;' output_path = os.path.join(current_dir, "ntga_model.xml") # Write our model to xml/{strategy_name}.xml with open(output_path, 'w') as file:
class BiasFinder(): def __init__(self, model_file, json_file, model_cardinalities): self.model = Network() self.model.load_state_dict(torch.load(model_file)) self.experiment_gen = TwinDataGenerator(model_cardinalities) self.output = self.gen_all_cond_probs() def approx_forward(self, input_torch_tensor): out_arr = self.model.forward(input_torch_tensor) largest = max(out_arr) for i in range(len(out_arr)): if out_arr[i] == largest: out_arr[i] = 1 else: out_arr[i] = 0 return out_arr def gen_attr_value_cond_prob(self, attr_index_value_dict): assert att result = {} arr = self.experiment_gen.gen_data_with_attr(attr_index, attr_value) output_size = len(self.model.forward(arr[0])) print(output_size) a = [] for i in range(output_size): a.append(0) for i in arr: res = self.approx_forward(i) indexofone = 0 for j in range(len(res)): if res[j] == 1: indexofone = j break a[indexofone] += 1 a = [1.0/len(arr) * i for i in a] print(a) for i in range(len(a)): key = "P(Output index is " + str(i) + "| Input indeces" + str(attr_index) + " has value " + str(attr_value) +")" result[key] = a[i] return result def gen_attr_cond_probs(self, attr_index): result = {} for i in self.experiment_gen.cardinalities[attr_index]: result.update(self.gen_attr_value_cond_prob(attr_index, i)) return result ''' def gen_all_cond_probs(self): result = {} for i in range(len(self.experiment_gen.cardinalities)): a = self.gen_attr_cond_probs(i) result.update(a) json_arr = [result] with open('ConditionalProbabilities.json', 'w') as outfile: json.dump(json_arr, outfile) return result ''' def gen_all_cond_probs(self): result = [] all_tensors = self.experiment_gen.get_all_options() print(len(all_tensors)) for option in all_tensors: input = TwinDataGenerator.get_tensor_from_dict(option) output = self.model.forward(input) for i in range(len(output)): current = "P[(Ouput is " + str(i) + ") | (inputs are: " + str(option) + ")]" result.append(current) return result
def __init__(self): Network.__init__(self)
import random import sys, pygame import time from math import sqrt, e VERBOSE_ERROR = True random.seed(time.time()) ############################################## # Network Setup # #===============# Network.DEFAULT_WEIGHT = 0 learnDecode = Network.create([1,6,6,11,11]) learnDecode.update() learnTruth = Network.create([4,8,8,4]) learnTruth.update() learnAdd = Network.create([2,4,4,4,4,1]) learnAdd.update() # network2 = network1.getReverse() # network2.display() #============================================#