Best Python code snippet using localstack_python
class_neural.py
Source:class_neural.py
1# импоÑÑиÑование библиоÑеки ÑипизаÑии даннÑÑ
2from typing import List, NoReturn, Union3# импоÑÑиÑование библиоÑеки Ð´Ð»Ñ ÑабоÑÑ Ñ Ð¼Ð°ÑÑиÑами, векÑоÑами4import numpy as np5# импоÑÑиÑование библиоÑеки Ð´Ð»Ñ Ð¿Ð¾Ð»ÑÑÐµÐ½Ð¸Ñ ÑÑнкÑии ÑигмоидÑ6import scipy.special as sc7# опÑеделение клаÑÑа нейÑонной ÑеÑи8class neuralNetwork:9 # иниÑиализиÑование нейÑонной ÑеÑи10 def __init__(self, input_nodes: int, hidden_nodes: List[int],11 output_nodes: int, learning_rate: float) -> NoReturn:12 # задаем колиÑеÑÑво Ñзлов во вÑ
одном, ÑкÑÑÑом и вÑÑ
одном ÑлоÑÑ
13 self.inodes = input_nodes14 self.hnodes = hidden_nodes15 self.onodes = output_nodes16 # коÑÑÑиÑÐ¸ÐµÐ½Ñ Ð¾Ð±ÑÑениÑ17 self.lr = learning_rate18 # ÑпиÑок маÑÑÐ¸Ñ Ð²ÐµÑовÑÑ
коÑÑиÑиенÑов19 self.w = self.create_w()20 # иÑполÑзование ÑÐ¸Ð³Ð¼Ð¾Ð¸Ð´Ñ Ð² каÑеÑÑве ÑÑнкÑии акÑиваÑии21 self.activation_function = lambda x: sc.expit(x)22 # Ñоздание ÑпиÑка маÑÑÐ¸Ñ Ð²ÐµÑовÑÑ
коÑÑиÑиенÑов23 def create_w(self) -> List[Union[np.ndarray, float]]:24 list_w = []25 """еÑли длина ÑкÑÑÑÑÑ
Ñлов 0, Ñо Ð¼Ñ Ñоздаем Ð¾Ð´Ð½Ñ Ð¼Ð°ÑÑиÑÑ Ð²ÐµÑов Ñ26 ÑазмеÑам колиÑеÑÑво вÑÑ
однÑÑ
нейÑонов и вÑ
однÑÑ
"""27 if len(self.hnodes) == 0:28 """запиÑÑваем в ÑпиÑок веÑов ÑгенеÑиÑованнÑе ÑлÑÑайнÑм обÑазом29 веÑовÑе коÑÑÑиÑиенÑÑ Ñ Ð¿Ñименением"""30 # ноÑмалÑного ÑаÑпÑÐµÐ´ÐµÐ»ÐµÐ½Ð¸Ñ Ð¸ Ñдвигом -0.531 list_w.append(np.random.normal(0.0, pow(self.onodes, -0.5),32 (self.onodes, self.inodes)))33 else:34 """инаÑе пÑоÑ
одим по ÑпиÑÐºÑ ÐºÐ¾Ð»Ð¸ÑеÑÑва нейÑонов ÑкÑÑÑом Ñлое и35 генеÑиÑÑем веÑовÑе коÑÑÑиÑиенÑÑ"""36 for i in range(len(self.hnodes)):37 if i == 0:38 list_w.append(np.random.normal(0.0,39 pow(self.hnodes[i], -0.5),40 (self.hnodes[i],41 self.inodes)))42 if len(self.hnodes) > 1:43 list_w.append(np.random.normal(0.0,44 pow(self.hnodes[i + 1],45 -0.5),46 (self.hnodes[i + 1],47 self.hnodes[i])))48 else:49 list_w.append(np.random.normal(0.0, pow(self.onodes,50 -0.5),51 (self.onodes,52 self.hnodes[i])))53 elif i == len(self.hnodes) - 1:54 list_w.append(np.random.normal(0.0, pow(self.onodes, -0.5),55 (self.onodes,56 self.hnodes[i])))57 else:58 list_w.append(np.random.normal(0.0, pow(self.hnodes[i + 1],59 -0.5),60 (self.hnodes[i + 1],61 self.hnodes[i])))62 return list_w63 # меÑод Ð´Ð»Ñ ÑÑениÑовки нейÑонной ÑеÑи64 def train(self, inputs_list: List[float], targets_list: List[float]) -> \65 NoReturn:66 # пÑеобÑазоваÑÑ ÑпиÑок вÑ
однÑÑ
в векÑоÑ67 inputs = np.array(inputs_list, ndmin=2).T68 targets = np.array(targets_list, ndmin=2).T69 outputs_list = []70 outputs = 071 i = 072 # полÑÑение вÑÑ
однÑÑ
Ñигналов на каждом Ñлое73 for hn in self.w:74 if i == 0:75 outputs = self.activation_function(np.dot(hn, inputs))76 else:77 outputs = self.activation_function(np.dot(hn, outputs))78 outputs_list.append(outputs)79 i += 180 # полÑÑение оÑибки в завиÑимоÑÑи Ð¾Ñ Ñелевого знаÑÐµÐ½Ð¸Ñ Ð½Ð° вÑÑ
оде81 output_errors = targets - outputs82 errors = []83 # пÑоÑеÑÑ Ð³ÑадиенÑного ÑпÑÑка в завиÑимоÑÑи Ð¾Ñ ÐºÐ¾Ð»Ð»Ð¸ÑеÑÑва ÑкÑÑÑÑ
Ñлоев84 if len(self.hnodes) == 0:85 self.w[0] += self.lr * np.dot((output_errors * outputs_list[0] *86 (1.0 - outputs_list[0])),87 np.transpose(inputs))88 else:89 i = 090 outputs_list = outputs_list[::-1]91 for out in outputs_list:92 if i == 0:93 self.w[-(i + 1)] += self.lr * np.dot((output_errors * out94 * (1.0 - out)),95 np.transpose(96 outputs_list[i +97 1]))98 elif i == len(outputs_list) - 1:99 if len(self.w) > 2:100 errors = np.dot(self.w[1].T, errors)101 self.w[0] += self.lr * np.dot((errors * out *102 (1.0 - out)),103 np.transpose(inputs))104 elif len(self.w) == 2:105 errors = np.dot(self.w[-1].T, output_errors)106 self.w[-(i + 1)] += self.lr * np.dot((errors *107 out * (1.0 -108 out)),109 np.transpose(110 inputs))111 elif i == 1:112 errors = np.dot(self.w[-1].T, output_errors)113 self.w[-(i + 1)] += self.lr * np.dot((errors * out *114 (1.0 - out)),115 np.transpose(116 outputs_list[i +117 1]))118 else:119 errors = np.dot(self.w[-i].T, errors)120 self.w[-(i + 1)] += self.lr * np.dot((errors * out *121 (1.0 - out)),122 np.transpose(123 outputs_list[i +124 1]))125 i += 1126 # опÑÐ¾Ñ Ð½ÐµÐ¹Ñонной ÑеÑи127 def query(self, inputs_list: List[float]) -> List[float]:128 # пÑеобÑазоваÑÑ ÑпиÑок вÑ
однÑÑ
знаÑений в двÑÑ
меÑнÑй маÑÑив129 inputs = np.array(inputs_list, ndmin=2).T130 # полÑÑение вÑÑ
одного Ñигнала131 for hn in self.w:132 inputs = self.activation_function(np.dot(hn, inputs))...
hill.py
Source:hill.py
1import numpy as np2def encrypt(messages, key, n):3 messages_list = [(ord(i) - ord('a') + 1) for i in messages]4 messages_list = np.reshape(messages_list, (n, n))5 key = np.reshape(key, (n, n))6 outputs_list = []7 for i in messages_list:8 outputs_list.append(np.dot(key, i) % 26)9 outputs_list = np.reshape(outputs_list, (1, n ** 2))[0]10 o = [chr(i + ord('a')) for i in outputs_list]11 print(''.join(o))12def isPerfectSquare(x):13 sr = np.sqrt(x)14 return ((sr - np.floor(sr)) == 0), np.ceil(sr - np.floor(sr))15def decrypt(messages_decdecrypted, key, n):16 messages_decdecrypted_list = [(ord(i) - ord('a')) for i in messages_decdecrypted]17 messages_list = np.reshape(messages_decdecrypted_list, (n, n))18 print(messages_decdecrypted_list)19 key = np.reshape(key, (n, n))20 # print(key)21 key = np.linalg.inv(key) % 2622 print(key)23 outputs_list = []24 for i in messages_list:25 outputs_list.append(np.dot(key, i) % 26)26 outputs_list = np.reshape(outputs_list, (1, n ** 2))[0]27 print(outputs_list)28 o = [chr(round(i) + ord('a')-1) for i in outputs_list]29 print(''.join(o))30choice=int(input("select\n 1 for encrypt\n 2 for decrypt"))31if choice==1:32 messages = input("enter the message: ")33 key = list(map(int, input("in 1xn spaces array: ").split(" ")))34 n=int(np.sqrt(len(key)))35 encrypt(messages,key,n)36else:37 messages = input("enter the encrypted message: ")38 key = list(map(int, input("in 1xn spaces array: ").split(" ")))39 n=int(np.sqrt(len(key)))40 decrypt(messages, key, n)41# messages = 'hwkb'#input('message')42# conditions = isPerfectSquare(len(key))43# print(conditions)...
tools.py
Source:tools.py
1## @package tools2# Module caffe2.python.helpers.tools3from __future__ import absolute_import4from __future__ import division5from __future__ import print_function6from __future__ import unicode_literals7def image_input(8 model, blob_in, blob_out, order="NCHW", use_gpu_transform=False, **kwargs9):10 assert 'is_test' in kwargs, "Argument 'is_test' is required"11 if order == "NCHW":12 if (use_gpu_transform):13 kwargs['use_gpu_transform'] = 1 if use_gpu_transform else 014 # GPU transform will handle NHWC -> NCHW15 outputs = model.net.ImageInput(blob_in, blob_out, **kwargs)16 pass17 else:18 outputs = model.net.ImageInput(19 blob_in, [blob_out[0] + '_nhwc'] + blob_out[1:], **kwargs20 )21 outputs_list = list(outputs)22 outputs_list[0] = model.net.NHWC2NCHW(outputs_list[0], blob_out[0])23 outputs = tuple(outputs_list)24 else:25 outputs = model.net.ImageInput(blob_in, blob_out, **kwargs)26 return outputs27def video_input(model, blob_in, blob_out, **kwargs):28 # size of outputs can vary depending on kwargs29 outputs = model.net.VideoInput(blob_in, blob_out, **kwargs)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!