def __init__(self,
                 teacher_path,
                 validation_path,
                 max_queries=100,
                 student_lr=0.2):
        self.sess = tf.Session()
        self.teacher_path = teacher_path
        self.student_lr = student_lr

        self.validation_inputs = np.load(
            os.path.join(validation_path, "inputs.npz")).reshape(-1, 1)
        self.validation_targets = np.load(
            os.path.join(validation_path, "targets.npz")).reshape(-1, 1)

        self.student_queries = 0
        self.max_queries = max_queries
        self.teacher_model = TeacherModel(input_dim=1,
                                          target_dim=1,
                                          layers=[12, 12, 8],
                                          activation=tf.nn.sigmoid,
                                          lr=1e-4)
        self.teacher_model.load(self.teacher_path)

        self.student_model = StudentModel(
            input_dim=1,
            target_dim=1,
            layers=[4, 4],
            activation=tf.nn.sigmoid,
            lr=self.
            student_lr  # TODO WOODY CHANGE BACK TO 0.01 if this doesn't work
        )
        # self.sess.run(tf.global_variables_initializer())

        self.num_queries = 1  # TODO
        self.action_space_low = -5  # TODO
        self.action_space_high = 5  # TODO
        self.observation_space_low = -np.inf  # TODO
        self.observation_space_high = np.inf  # TODO

        self.action_space = spaces.Box(
            low=self.action_space_low,
            high=self.action_space_high,
            shape=(self.num_queries, )  # number of examples we're querying
        )

        # WOODY ATTEMPT 1 STATE
        '''self.observation_space = spaces.Box(
            low=self.observation_space_low,
            high=self.observation_space_high,
            shape=(self.student_model.num_weights + 4, )
        )'''
        self.history_len = 10
        self.observation_space = spaces.Box(low=self.observation_space_low,
                                            high=self.observation_space_high,
                                            shape=(4 * self.history_len, ))

        _, self.previous_val_loss = self.student_model.validate(
            self.validation_inputs, self.validation_targets)

        self.state_queue = [0.0, 0.0, 0.0, 0.0] * self.history_len
    def reset(self):
        """
        Upon reaching end of episode, go back to
        initialization state
        """
        self.student_queries = 0
        # WOODY ATTEMPT 1 STATE
        #obs = np.hstack([self.student_model.weights, np.zeros((4,))])
        self.state_queue = [0.0, 0.0, 0.0, 0.0] * self.history_len
        obs = np.zeros((4 * self.history_len, ))
        tf.reset_default_graph()
        self.teacher_model = TeacherModel(input_dim=1,
                                          target_dim=1,
                                          layers=[12, 12, 8],
                                          activation=tf.nn.sigmoid,
                                          lr=1e-4)
        self.teacher_model.load(self.teacher_path)

        self.student_model = StudentModel(input_dim=1,
                                          target_dim=1,
                                          layers=[4, 4],
                                          activation=tf.nn.sigmoid,
                                          lr=self.student_lr)
        _, self.previous_val_loss = self.student_model.validate(
            self.validation_inputs, self.validation_targets)
        return obs
Example #3
0
    def delete(self):
        user = UserModel.get_by_username(get_jwt_identity())
        data = request.get_json()
        if not user.is_admin:
            return {MESSAGE: "admin privilege required"}, 400

        if not ClassroomModel.remove({CLASSROOM_NAME: data[CLASSROOM_NAME]}):
            return {MESSAGE: "invalid data"}, 400
        else:
            StudentModel.remove({CLASSROOM_NAME: data[CLASSROOM_NAME]})
            return {MESSAGE: "classroom deleted"}, 200
Example #4
0
    def __init__(self,
                 teacher_path,
                 validation_path,
                 max_queries=100,
                 student_lr=0.1):
        self.sess = tf.Session()
        self.teacher_path = teacher_path
        self.student_lr = student_lr

        self.validation_inputs = np.load(
            os.path.join(validation_path, "inputs.npz")).reshape(-1, 1)
        self.validation_targets = np.load(
            os.path.join(validation_path, "targets.npz")).reshape(-1, 1)

        self.student_queries = 0
        self.max_queries = max_queries
        self.teacher_model = TeacherModel(input_dim=1,
                                          target_dim=1,
                                          layers=[12, 12, 8],
                                          activation=tf.nn.sigmoid,
                                          lr=1e-4)
        self.teacher_model.load(self.teacher_path)

        self.student_model = StudentModel(input_dim=1,
                                          target_dim=1,
                                          layers=[4, 4],
                                          activation=tf.nn.sigmoid,
                                          lr=self.student_lr)
        # self.sess.run(tf.global_variables_initializer())

        self.num_queries = 1  # TODO
        self.action_space_low = -5  # TODO
        self.action_space_high = 5  # TODO
        self.observation_space_low = -np.inf  # TODO
        self.observation_space_high = np.inf  # TODO

        self.num_discrete_actions = 1000
        self.action_space = spaces.Discrete(
            self.num_discrete_actions
        )  # Set with 11 elements {0, 1, 2, ..., 10}
        self.actions = np.linspace(-5, 5, self.num_discrete_actions)

        self.history_len = 10
        self.observation_space = spaces.Box(low=self.observation_space_low,
                                            high=self.observation_space_high,
                                            shape=(4 * self.history_len, ))

        _, self.previous_val_loss = self.student_model.validate(
            self.validation_inputs, self.validation_targets)

        self.state_queue = [0.0, 0.0, 0.0, 0.0] * self.history_len
Example #5
0
    def put(self, stude_id):
        data = Student.parser.parse_args()
        item = StudentModel.find_by_id(stude_id)

        if item is None:
            item = StudentModel(stude_id, **data)
        else:
            item.name = data['name']
            item.course = data['course']
            item.year = data['year']
            item.gender = data['gender']

        item.save_to_db()

        return item.json()
Example #6
0
def update_students(classroom):
    StudentModel.remove({CLASSROOM_NAME: classroom.classroom_name})
    for username in classroom.user_list:
        user = UserModel.get_by_username(username)
        if not user:
            print("not found user: "******"no student found with username = "******" in classroom " + str(classroom.classroom_name))
            continue
        long_contests = []
        for contest in classroom.vjudge_contest_list:
            long_contests.append({
                "contest_title":
                contest["contest_title"],
                "contest_id":
                contest["contest_id"],
                "total_problems":
                contest["total_problems"],
                "minimum_solve_required":
                contest["minimum_solve_required"],
                "solved_problems":
                vjudge_sraper.solve_details_in_contest_from_data(
                    data=data_map[contest[CONTEST_ID]],
                    username=vjudge_handle),
                "contest_type":
                contest["contest_type"]
            })
        new_values = {"long_contests": long_contests}
        student.update_to_mongo(new_values)
Example #7
0
class AdminController(Observer):
    def __init__(self, master, state):
        self.master = master
        self.state = state
        self.electives = Elective()
        print(1)
        self.state.add_observer(self)

    def load_model_view(self, id):
        self.view = AdminView(self.master)
        self.model = StudentModel(id, self.electives)
        self.model.add_observer(self)
        self.render_view()

    def render_view(self):
        self.view.admin_id['text'] = self.model.student['id']
        self.view.admin_name['text'] = self.model.student['name']
        self.view.admin_type['text'] = self.model.student['type']
        self.load_elective_list()

    def load_elective_list(self):
        self.view.elective_listbox.delete(0, END)
        for i in self.electives.electives:
            self.view.elective_listbox.insert(END, i)
        self.view.elective_listbox.bind('<<ListboxSelect>>',
                                        self.load_student_list)

    def load_student_list(self, e):
        elective = self.view.elective_listbox.get(ACTIVE)
        self.view.student_listbox.delete(0, END)
        for student in self.electives.electives[elective]['students']:
            self.view.student_listbox.insert(END, student)

    def update(self, obj, **kwargs):
        state_alert = kwargs.keys()
        print('admin')
        if 'state' in state_alert:
            if kwargs['state']['type'] == 'admin':
                self.load_model_view(kwargs['state']['id'])
        if 'electives' in state_alert:
            self.render_view()
Example #8
0
 def put(self):
     user = UserModel.get_by_username(get_jwt_identity())
     data = request.get_json()
     if not user.is_admin:
         return {MESSAGE: "admin privilege required"}, 400
     if not data or CLASSROOM_NAME not in data:
         return {MESSAGE: "invalid data"}, 400
     student = StudentModel.get_by_username_and_classroom_name(
         data[USERNAME], data[CLASSROOM_NAME])
     if not student:
         return {MESSAGE: "student not found"}, 404
     student.update_to_mongo(data)
     return {MESSAGE: "data updated"}, 200
    def post(self):
        data = request.get_json()
        username = get_jwt_identity()
        if data and USERNAME in data:
            username = data[USERNAME]
        classroom_name = ""
        if "classroom_name" in data:
            classroom_name = data["classroom_name"]
        else:
            classroom_list = ClassroomModel.get_all_classrooms({})
            # print(username, classroom_list)
            for classroom in classroom_list:
                if username not in classroom["user_list"]:
                    continue
                curr_name = classroom["classroom_name"]
                if curr_name == "rated":
                    classroom_name = curr_name
                    break
                else:
                    if classroom_name == "":
                        classroom_name = curr_name
                    else:
                        if int(curr_name[1, len(curr_name) - 1]) > int(
                                classroom_name[1, len(classroom_name) - 1]):
                            classroom_name = curr_name
        student = StudentModel.get_by_username_and_classroom_name(
            username, classroom_name)
        if not student:
            return {MESSAGE: "no data found"}, 404

        res = student.json()

        vjudge_username = OjModel.get_vjudge_username(username)
        if not vjudge_username:
            return res, 200

        contest_data = ContestDataModel.get_vjudge_contest_data()
        last_day_solve = get_last_day_solve(contest_data.data, vjudge_username)
        last_30_days_solve = get_last_30_days_solve(contest_data.data,
                                                    vjudge_username)
        todo_list = get_todos(username, classroom_name)
        announcement_list = get_announcements(username, classroom_name)

        res["last_day_solve"] = last_day_solve
        res["last_30_days_solve"] = last_30_days_solve
        res["todo_list"] = todo_list
        res["announcement_list"] = announcement_list
        return res, 200
Example #10
0
    def post(self, stude_id):
        if StudentModel.find_by_id(stude_id):
            return {
                'message': "Student ID '{}' already exist.".format(stude_id)
            }, 400

        data = request.get_json()
        item = StudentModel(stude_id, **data)

        try:
            item.save_to_db()
        except:
            return {"message": "An error occured in inserting the item."}, 500

        return item.json(), 201
Example #11
0
class TeachingEnvDiscrete(gym.Env):
    def __init__(self,
                 teacher_path,
                 validation_path,
                 max_queries=100,
                 student_lr=0.1):
        self.sess = tf.Session()
        self.teacher_path = teacher_path
        self.student_lr = student_lr

        self.validation_inputs = np.load(
            os.path.join(validation_path, "inputs.npz")).reshape(-1, 1)
        self.validation_targets = np.load(
            os.path.join(validation_path, "targets.npz")).reshape(-1, 1)

        self.student_queries = 0
        self.max_queries = max_queries
        self.teacher_model = TeacherModel(input_dim=1,
                                          target_dim=1,
                                          layers=[12, 12, 8],
                                          activation=tf.nn.sigmoid,
                                          lr=1e-4)
        self.teacher_model.load(self.teacher_path)

        self.student_model = StudentModel(input_dim=1,
                                          target_dim=1,
                                          layers=[4, 4],
                                          activation=tf.nn.sigmoid,
                                          lr=self.student_lr)
        # self.sess.run(tf.global_variables_initializer())

        self.num_queries = 1  # TODO
        self.action_space_low = -5  # TODO
        self.action_space_high = 5  # TODO
        self.observation_space_low = -np.inf  # TODO
        self.observation_space_high = np.inf  # TODO

        self.num_discrete_actions = 1000
        self.action_space = spaces.Discrete(
            self.num_discrete_actions
        )  # Set with 11 elements {0, 1, 2, ..., 10}
        self.actions = np.linspace(-5, 5, self.num_discrete_actions)

        self.history_len = 10
        self.observation_space = spaces.Box(low=self.observation_space_low,
                                            high=self.observation_space_high,
                                            shape=(4 * self.history_len, ))

        _, self.previous_val_loss = self.student_model.validate(
            self.validation_inputs, self.validation_targets)

        self.state_queue = [0.0, 0.0, 0.0, 0.0] * self.history_len

    def step(self, action):
        """
        Action is a (1,) numpy array
        """
        action = np.asarray([np.asscalar(self.actions[np.asscalar(action)])
                             ])  # Shape (1,)
        action = action.reshape(-1, 1)  # dimensionality case
        #print(action) #scale action back
        teacher_output = self.teacher_model(action)
        student_output, MSE_loss, _ = self.student_model.train_step(
            action, teacher_output)
        _, validation_loss = self.student_model.validate(
            self.validation_inputs, self.validation_targets)
        self.state_queue.append(np.asscalar(action))
        self.state_queue.append(np.asscalar(teacher_output))
        self.state_queue.append(np.asscalar(student_output))
        self.state_queue.append(MSE_loss)
        for i in range(4):
            self.state_queue.pop(0)
        obs = np.asarray(self.state_queue)
        info = {}
        self.student_queries += 1
        done = (self.student_queries == self.max_queries)
        reward = self.previous_val_loss - validation_loss
        self.previous_val_loss = validation_loss
        # state, reward, isterminal, metadata
        return (obs, reward, done, info)

    def reset(self):
        """
        Upon reaching end of episode, go back to
        initialization state
        """
        self.student_queries = 0
        self.state_queue = [0.0, 0.0, 0.0, 0.0] * self.history_len
        obs = np.zeros((4 * self.history_len, ))
        tf.reset_default_graph()
        self.teacher_model = TeacherModel(input_dim=1,
                                          target_dim=1,
                                          layers=[12, 12, 8],
                                          activation=tf.nn.sigmoid,
                                          lr=1e-4)
        self.teacher_model.load(self.teacher_path)

        self.student_model = StudentModel(input_dim=1,
                                          target_dim=1,
                                          layers=[4, 4],
                                          activation=tf.nn.sigmoid,
                                          lr=self.student_lr)
        _, self.previous_val_loss = self.student_model.validate(
            self.validation_inputs, self.validation_targets)
        return obs

    def render(self, mode='human', close=False):
        pass
Example #12
0
    def delete(self, stude_id):
        item = StudentModel.find_by_id(stude_id)
        if item:
            item.delete_from_db()

        return {'message': 'Item deleted'}
Example #13
0
 def get(self, stude_id):
     item = StudentModel.find_by_id(stude_id)
     if item:
         return item.json()
     return {'message': 'Item not found'}, 404
class TeachingEnv(gym.Env):
    def __init__(self,
                 teacher_path,
                 validation_path,
                 max_queries=100,
                 student_lr=0.2):
        self.sess = tf.Session()
        self.teacher_path = teacher_path
        self.student_lr = student_lr

        self.validation_inputs = np.load(
            os.path.join(validation_path, "inputs.npz")).reshape(-1, 1)
        self.validation_targets = np.load(
            os.path.join(validation_path, "targets.npz")).reshape(-1, 1)

        self.student_queries = 0
        self.max_queries = max_queries
        self.teacher_model = TeacherModel(input_dim=1,
                                          target_dim=1,
                                          layers=[12, 12, 8],
                                          activation=tf.nn.sigmoid,
                                          lr=1e-4)
        self.teacher_model.load(self.teacher_path)

        self.student_model = StudentModel(
            input_dim=1,
            target_dim=1,
            layers=[4, 4],
            activation=tf.nn.sigmoid,
            lr=self.
            student_lr  # TODO WOODY CHANGE BACK TO 0.01 if this doesn't work
        )
        # self.sess.run(tf.global_variables_initializer())

        self.num_queries = 1  # TODO
        self.action_space_low = -5  # TODO
        self.action_space_high = 5  # TODO
        self.observation_space_low = -np.inf  # TODO
        self.observation_space_high = np.inf  # TODO

        self.action_space = spaces.Box(
            low=self.action_space_low,
            high=self.action_space_high,
            shape=(self.num_queries, )  # number of examples we're querying
        )

        # WOODY ATTEMPT 1 STATE
        '''self.observation_space = spaces.Box(
            low=self.observation_space_low,
            high=self.observation_space_high,
            shape=(self.student_model.num_weights + 4, )
        )'''
        self.history_len = 10
        self.observation_space = spaces.Box(low=self.observation_space_low,
                                            high=self.observation_space_high,
                                            shape=(4 * self.history_len, ))

        _, self.previous_val_loss = self.student_model.validate(
            self.validation_inputs, self.validation_targets)

        self.state_queue = [0.0, 0.0, 0.0, 0.0] * self.history_len

    def step(self, action):
        """
        Action is a (1,) numpy array
        """
        action = action.reshape(-1, 1)  # dimensionality case
        teacher_output = self.teacher_model(action)
        student_output, MSE_loss, _ = self.student_model.train_step(
            action, teacher_output)
        _, validation_loss = self.student_model.validate(
            self.validation_inputs, self.validation_targets)
        '''obs = np.hstack(
            [
                self.student_model.weights,
                np.array(
                    [
                        np.asscalar(action),
                        np.asscalar(teacher_output),
                        np.asscalar(student_output),
                        MSE_loss
                    ]
                )
            ]
        )'''
        # WOODY ATTEMPT 1 STATE
        self.state_queue.append(np.asscalar(action))
        self.state_queue.append(np.asscalar(teacher_output))
        self.state_queue.append(np.asscalar(student_output))
        self.state_queue.append(MSE_loss)
        for i in range(4):
            self.state_queue.pop(0)
        obs = np.asarray(self.state_queue)

        info = {}
        self.student_queries += 1
        done = (self.student_queries == self.max_queries)
        # WOODY ATTEMPT 1
        reward = self.previous_val_loss - validation_loss
        self.previous_val_loss = validation_loss

        # state, reward, isterminal, metadata
        return (obs, reward, done, info)

    def reset(self):
        """
        Upon reaching end of episode, go back to
        initialization state
        """
        self.student_queries = 0
        # WOODY ATTEMPT 1 STATE
        #obs = np.hstack([self.student_model.weights, np.zeros((4,))])
        self.state_queue = [0.0, 0.0, 0.0, 0.0] * self.history_len
        obs = np.zeros((4 * self.history_len, ))
        tf.reset_default_graph()
        self.teacher_model = TeacherModel(input_dim=1,
                                          target_dim=1,
                                          layers=[12, 12, 8],
                                          activation=tf.nn.sigmoid,
                                          lr=1e-4)
        self.teacher_model.load(self.teacher_path)

        self.student_model = StudentModel(input_dim=1,
                                          target_dim=1,
                                          layers=[4, 4],
                                          activation=tf.nn.sigmoid,
                                          lr=self.student_lr)
        _, self.previous_val_loss = self.student_model.validate(
            self.validation_inputs, self.validation_targets)
        return obs

    def render(self, mode='human', close=False):
        pass
Example #15
0
 def load_model_view(self, id):
     self.view = StudentView(self.master)
     self.model = StudentModel(id, self.electives)
     self.model.add_observer(self)
     self.render_view()
Example #16
0
 def post(self):
     data = request.get_json()
     student = StudentModel.get_all_students(data)
     if not student:
         return {MESSAGE: "Student Not Found"}, 404
     return student, 200
Example #17
0
class StudentController(Observer):
    def __init__(self, master, state):
        self.master = master
        self.state = state
        self.electives = Elective()
        self.state.add_observer(self)

    def load_model_view(self, id):
        self.view = StudentView(self.master)
        self.model = StudentModel(id, self.electives)
        self.model.add_observer(self)
        self.render_view()

    def render_view(self):
        self.view.stu_id['text'] = self.model.student['id']
        self.view.stu_name['text'] = self.model.student['name']
        self.view.stu_type['text'] = self.model.student['type']
        self.view.stu_term['text'] = self.model.student['term']

        self.view.add_button.configure(command=self.add_elective)
        self.view.delete_button.configure(command=self.delete_elective)
        self.view.elec_option_listbox.bind('<<ListboxSelect>>',
                                           self.show_description)
        self.load_electivebox()
        self.load_elec_options()

    def show_description(self, e):
        selection = self.view.elec_option_listbox.get(ACTIVE)
        messagebox.showinfo(
            title=self.electives.electives[selection]['name'],
            message=self.electives.electives[selection]['description'])
        # self.view.elective_name['text'] = self.electives.electives[selection]['name']
        # self.view.elective_description['text'] = self.electives.electives[selection]['description']

    def load_electivebox(self):
        electives = self.model.student['electives']
        self.view.elective_listbox.delete(0, END)
        for i in electives:
            self.view.elective_listbox.insert(END, i)

    def load_elec_options(self):
        self.view.elec_option_listbox.delete(0, END)
        for elective in self.electives.electives:
            self.view.elec_option_listbox.insert(END, elective)

    def add_elective(self):
        new_elective = self.view.elec_option_listbox.get(ACTIVE)
        if self.model.add_elective(new_elective):
            self.electives.add_student_to_elec(new_elective, self.model.id)

    def delete_elective(self):
        sel_elective = self.view.elective_listbox.get(ACTIVE)
        if self.model.remove_elective(sel_elective):
            self.electives.remove_student(sel_elective, self.model.id)

    def update(self, obj, **kwargs):
        state_alert = kwargs.keys()
        print(state_alert)
        if 'state' in state_alert:
            if kwargs['state']['type'] == 'student':
                self.load_model_view(kwargs['state']['id'])
        if 'electives' in state_alert:
            self.render_view()