def from_weights(cls, layers, weights, biases): """ Создание агента по параметрам его нейронной сети. Разбираться не обязательно. """ agent = SimpleCarAgent() agent._rays = weights[0].shape[1] - 4 nn = Network(layers, output_function=lambda x: x, output_derivative=lambda x: 1) if len(weights) != len(nn.weights): raise AssertionError("You provided %d weight matrices instead of %d" % (len(weights), len(nn.weights))) for i, (w, right_w) in enumerate(zip(weights, nn.weights)): if w.shape != right_w.shape: raise AssertionError("weights[%d].shape = %s instead of %s" % (i, w.shape, right_w.shape)) nn.weights = weights if len(biases) != len(nn.biases): raise AssertionError("You provided %d bias vectors instead of %d" % (len(weights), len(nn.weights))) for i, (b, right_b) in enumerate(zip(biases, nn.biases)): if b.shape != right_b.shape: raise AssertionError("biases[%d].shape = %s instead of %s" % (i, b.shape, right_b.shape)) nn.biases = biases agent.neural_net = nn return agent
def from_weights(cls, layers, weights, biases): """ Создание агента по параметрам его нейронной сети. Разбираться не обязательно. """ agent = SimpleCarAgent() agent._rays = weights[0].shape[1] - 4 nn = Network(layers, output_function=lambda x: x, output_derivative=lambda x: 1) if len(weights) != len(nn.weights): raise AssertionError( "You provided %d weight matrices instead of %d" % (len(weights), len(nn.weights))) for i, (w, right_w) in enumerate(zip(weights, nn.weights)): if w.shape != right_w.shape: raise AssertionError("weights[%d].shape = %s instead of %s" % (i, w.shape, right_w.shape)) nn.weights = weights if len(biases) != len(nn.biases): raise AssertionError("You provided %d bias vectors instead of %d" % (len(weights), len(nn.weights))) for i, (b, right_b) in enumerate(zip(biases, nn.biases)): if b.shape != right_b.shape: raise AssertionError("biases[%d].shape = %s instead of %s" % (i, b.shape, right_b.shape)) nn.biases = biases agent.neural_net = nn return agent
def from_weights(cls, layers: List[int], weights, biases): """ Creates agent by neural network params. """ agent = SimpleCarAgent() agent._rays = weights[0].shape[1] - NUM_DEFAULT_INPUT_NEURONS nn = Network(layers, output_function=lambda x: x, output_derivative=lambda x: 1) if len(weights) != len(nn.weights): raise AssertionError( "You provided %d weight matrices instead of %d" % (len(weights), len(nn.weights))) for i, (w, right_w) in enumerate(zip(weights, nn.weights)): if w.shape != right_w.shape: raise AssertionError("weights[%d].shape = %s instead of %s" % (i, w.shape, right_w.shape)) nn.weights = weights if len(biases) != len(nn.biases): raise AssertionError("You provided %d bias vectors instead of %d" % (len(weights), len(nn.weights))) for i, (b, right_b) in enumerate(zip(biases, nn.biases)): if b.shape != right_b.shape: raise AssertionError("biases[%d].shape = %s instead of %s" % (i, b.shape, right_b.shape)) nn.biases = biases agent.neural_net = nn return agent