Esempio n. 1
0
class QModel(object):
    def __init__(self, config):
        self.features = FeatureFunctionVector(config["features"])
        self.initialize_weights(config["initial_weights"])

    def initialize_weights(self, config):
        n = self.features.length()
        if config["type"] == "uniform_random":
            self.weights = (np.random.random((n,)) * 2.0) - 1.0
        elif config["type"] == "zero":
            self.weights = np.zeros((n,))
        elif config["type"] == "explicit":
            name_to_weights = config["values"]
            names_and_lengths = self.features.names_and_lengths()
            assert set(name_to_weights.keys()) == set(list(n for n, l in names_and_lengths))
            weights_list = []
            for name, length in names_and_lengths:
                ws = name_to_weights[name]
                assert length == len(
                    ws
                ), "Length {} does not equal number of weights specified {} for weight named {}".format(
                    length, len(ws), name
                )
                weights_list.extend(ws)
            self.weights = np.array(weights_list, dtype=np.float_)
        else:
            raise Exception(
                "Unexpected type: {}. Possibilities are: {}".format(
                    config["type"], ["uniform_random", "zero", "explicit"]
                )
            )

    def q(self, s, a):
        return self.weights.dot(self.features.f(s, a))

    def beta(self, s, a):
        return self.features.f(s, a)

    def update_weights(self, delta):
        self.weights += delta

    def save_model(self, fn):
        np.savetxt(fn, self.weights)
class QModel(object):
    def __init__(self, config):
        self.features = FeatureFunctionVector(config["features"])
        self.initialize_weights(config["initial_weights"])

    def initialize_weights(self, config):
        n = self.features.length()
        if config["type"] == "uniform_random":
            self.weights = (np.random.random((n, )) * 2.0) - 1.0
        elif config["type"] == "zero":
            self.weights = np.zeros((n, ))
        elif config["type"] == "explicit":
            name_to_weights = config["values"]
            names_and_lengths = self.features.names_and_lengths()
            assert set(name_to_weights.keys()) == set(
                list(n for n, l in names_and_lengths))
            weights_list = []
            for name, length in names_and_lengths:
                ws = name_to_weights[name]
                assert length == len(
                    ws
                ), "Length {} does not equal number of weights specified {} for weight named {}".format(
                    length, len(ws), name)
                weights_list.extend(ws)
            self.weights = np.array(weights_list, dtype=np.float_)
        else:
            raise Exception(
                "Unexpected type: {}. Possibilities are: {}".format(
                    config["type"], ["uniform_random", "zero", "explicit"]))

    def q(self, s, a):
        return self.weights.dot(self.features.f(s, a))

    def beta(self, s, a):
        return self.features.f(s, a)

    def update_weights(self, delta):
        self.weights += delta

    def save_model(self, fn):
        np.savetxt(fn, self.weights)
 def __init__(self, config):
     self.features = FeatureFunctionVector(config["features"])
     self.initialize_weights(config["initial_weights"])
Esempio n. 4
0
 def __init__(self, config):
     self.features = FeatureFunctionVector(config["features"])
     self.initialize_weights(config["initial_weights"])