From 5a4e48d86c8935851dda76c2d583bdc384b45665 Mon Sep 17 00:00:00 2001 From: Dominik Roth Date: Fri, 24 Sep 2021 19:12:09 +0200 Subject: [PATCH] Added a regression-loss (push weights towards 1) --- caliGraph.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/caliGraph.py b/caliGraph.py index c370ac6..3d82115 100755 --- a/caliGraph.py +++ b/caliGraph.py @@ -341,7 +341,7 @@ def scoreOpinions(G, globMu, globStd): def scoreUnread(G, globMu, globStd): for n in list(G.nodes): feedbacks = [globMu] - weights = [getWeightForType('mu')] + ws = [getWeightForType('mu')] node = G.nodes[n] if node['t'] == 'book': if node['rating'] == None: @@ -352,16 +352,16 @@ def scoreUnread(G, globMu, globStd): w = getWeightForType(adjNode['t'], G[n][adj]['weight'] if 'weight' in G[n][adj] else 1) for fb in adjNode['feedbacks']: feedbacks.append(fb) - weights.append(w) + ws.append(w) if len(feedbacks): node['mean'], node['std'] = norm.fit(feedbacks) node['se'] = globStd / math.sqrt(len(feedbacks)) feedbacks.append(node['std']) - weights.append(getWeightForType('sigma')) + ws.append(getWeightForType('sigma')) feedbacks.append(node['se']) - weights.append(getWeightForType('se')) - node['score'] = sum([fb*w for fb, w in zip(feedbacks, weights)])/len(feedbacks) - #node['score'] = sum([fb*w for fb, w in zip(feedbacks, weights)])/sum(weights) + ws.append(getWeightForType('se')) + #node['score'] = sum([fb*w for fb, w in zip(feedbacks, weights)])/len(feedbacks) + node['score'] = sum([fb*w for fb, w in zip(feedbacks, ws)])/len(feedbacks) else: node['score'] = globMu + errorFac*globStd + len(feedbacks)*0.0000000001 if 'series' in node: @@ -683,6 +683,7 @@ def waveFlow(G, node, n, dist, menge, firstEdge=False): waveFlow(G, node, m, dist, menge, firstEdge=firstEdge) def evaluateFitness(books): + global weights G = buildBookGraph(books) graphAddAuthors(G, books) graphAddRecommenders(G, books) @@ -703,7 +704,8 @@ def evaluateFitness(books): if rating > 10.0: errSq[-1] *= 1.5 G.nodes[m]['rating'] = rating - return sum(errSq) / len(errSq) + regressionLoss = sum([(1-w)**2 for w in weights.values()]) + return sum(errSq)/len(errSq) + regressionLoss/1000 def train(gamma = 0.1): global weights @@ -720,15 +722,15 @@ def train(gamma = 0.1): if gamma < 0.01: while random.random() < 0.5: attr = random.choice(w) - weights[attr] = -0.1+random.random()*1.5 + weights[attr] = -0.1+random.random()*1.5+random.random() else: weights[attr] += delta if attr not in ['sigma', 'mu', 'se']: - weights[attr] = min(max(0, weights[attr]), 3) + weights[attr] = min(max(0.0, weights[attr]), 3.0) mse = evaluateFitness(books) if mse < best_mse: # got better saveWeights(weights) - gamma = max(gamma*1.75, 0.001) + gamma = min(max(gamma*1.75, 0.001), 0.5) bestWeights = copy.copy(weights) best_mse = mse delta *= 2