Added a regression-loss (push weights towards 1)
This commit is contained in:
parent
9d6f37af45
commit
5a4e48d86c
22
caliGraph.py
22
caliGraph.py
@ -341,7 +341,7 @@ def scoreOpinions(G, globMu, globStd):
|
|||||||
def scoreUnread(G, globMu, globStd):
|
def scoreUnread(G, globMu, globStd):
|
||||||
for n in list(G.nodes):
|
for n in list(G.nodes):
|
||||||
feedbacks = [globMu]
|
feedbacks = [globMu]
|
||||||
weights = [getWeightForType('mu')]
|
ws = [getWeightForType('mu')]
|
||||||
node = G.nodes[n]
|
node = G.nodes[n]
|
||||||
if node['t'] == 'book':
|
if node['t'] == 'book':
|
||||||
if node['rating'] == None:
|
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)
|
w = getWeightForType(adjNode['t'], G[n][adj]['weight'] if 'weight' in G[n][adj] else 1)
|
||||||
for fb in adjNode['feedbacks']:
|
for fb in adjNode['feedbacks']:
|
||||||
feedbacks.append(fb)
|
feedbacks.append(fb)
|
||||||
weights.append(w)
|
ws.append(w)
|
||||||
if len(feedbacks):
|
if len(feedbacks):
|
||||||
node['mean'], node['std'] = norm.fit(feedbacks)
|
node['mean'], node['std'] = norm.fit(feedbacks)
|
||||||
node['se'] = globStd / math.sqrt(len(feedbacks))
|
node['se'] = globStd / math.sqrt(len(feedbacks))
|
||||||
feedbacks.append(node['std'])
|
feedbacks.append(node['std'])
|
||||||
weights.append(getWeightForType('sigma'))
|
ws.append(getWeightForType('sigma'))
|
||||||
feedbacks.append(node['se'])
|
feedbacks.append(node['se'])
|
||||||
weights.append(getWeightForType('se'))
|
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, weights)])/len(feedbacks)
|
||||||
#node['score'] = sum([fb*w for fb, w in zip(feedbacks, weights)])/sum(weights)
|
node['score'] = sum([fb*w for fb, w in zip(feedbacks, ws)])/len(feedbacks)
|
||||||
else:
|
else:
|
||||||
node['score'] = globMu + errorFac*globStd + len(feedbacks)*0.0000000001
|
node['score'] = globMu + errorFac*globStd + len(feedbacks)*0.0000000001
|
||||||
if 'series' in node:
|
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)
|
waveFlow(G, node, m, dist, menge, firstEdge=firstEdge)
|
||||||
|
|
||||||
def evaluateFitness(books):
|
def evaluateFitness(books):
|
||||||
|
global weights
|
||||||
G = buildBookGraph(books)
|
G = buildBookGraph(books)
|
||||||
graphAddAuthors(G, books)
|
graphAddAuthors(G, books)
|
||||||
graphAddRecommenders(G, books)
|
graphAddRecommenders(G, books)
|
||||||
@ -703,7 +704,8 @@ def evaluateFitness(books):
|
|||||||
if rating > 10.0:
|
if rating > 10.0:
|
||||||
errSq[-1] *= 1.5
|
errSq[-1] *= 1.5
|
||||||
G.nodes[m]['rating'] = rating
|
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):
|
def train(gamma = 0.1):
|
||||||
global weights
|
global weights
|
||||||
@ -720,15 +722,15 @@ def train(gamma = 0.1):
|
|||||||
if gamma < 0.01:
|
if gamma < 0.01:
|
||||||
while random.random() < 0.5:
|
while random.random() < 0.5:
|
||||||
attr = random.choice(w)
|
attr = random.choice(w)
|
||||||
weights[attr] = -0.1+random.random()*1.5
|
weights[attr] = -0.1+random.random()*1.5+random.random()
|
||||||
else:
|
else:
|
||||||
weights[attr] += delta
|
weights[attr] += delta
|
||||||
if attr not in ['sigma', 'mu', 'se']:
|
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)
|
mse = evaluateFitness(books)
|
||||||
if mse < best_mse: # got better
|
if mse < best_mse: # got better
|
||||||
saveWeights(weights)
|
saveWeights(weights)
|
||||||
gamma = max(gamma*1.75, 0.001)
|
gamma = min(max(gamma*1.75, 0.001), 0.5)
|
||||||
bestWeights = copy.copy(weights)
|
bestWeights = copy.copy(weights)
|
||||||
best_mse = mse
|
best_mse = mse
|
||||||
delta *= 2
|
delta *= 2
|
||||||
|
Loading…
Reference in New Issue
Block a user