Small tweaks to the scroring-algo and less calls to calibre when
training
This commit is contained in:
parent
54f82c024e
commit
9318811d8a
40
caliGraph.py
40
caliGraph.py
@ -316,7 +316,7 @@ def scoreOpinions(G, globMu, globStd, errorFac=0):
|
|||||||
for n in list(G.nodes):
|
for n in list(G.nodes):
|
||||||
node = G.nodes[n]
|
node = G.nodes[n]
|
||||||
feedbacks = []
|
feedbacks = []
|
||||||
if node['t'] in ['topList', 'recommender', 'author', 'series', 'tag']:
|
if node['t'] not in ['book']:
|
||||||
adjacens = list(G.adj[n].keys())
|
adjacens = list(G.adj[n].keys())
|
||||||
for adj in adjacens:
|
for adj in adjacens:
|
||||||
adjNode = G.nodes[adj]
|
adjNode = G.nodes[adj]
|
||||||
@ -351,10 +351,12 @@ def scoreUnread(G, globMu, globStd, errorFac=-0.6):
|
|||||||
weights.append(w)
|
weights.append(w)
|
||||||
if len(feedbacks):
|
if len(feedbacks):
|
||||||
node['meanUnweighted'], node['std'] = norm.fit(feedbacks)
|
node['meanUnweighted'], node['std'] = norm.fit(feedbacks)
|
||||||
|
node['se'] = globStd / math.sqrt(len(feedbacks))
|
||||||
feedbacks.append(node['std'])
|
feedbacks.append(node['std'])
|
||||||
weights.append(getWeightForType('sigma'))
|
weights.append(getWeightForType('sigma'))
|
||||||
|
feedbacks.append(1-1/len(feedbacks))
|
||||||
|
weights.append(getWeightForType('stability'))
|
||||||
node['mean'] = sum([fb*w for fb, w in zip(feedbacks, weights)])/len(feedbacks)
|
node['mean'] = sum([fb*w for fb, w in zip(feedbacks, weights)])/len(feedbacks)
|
||||||
node['se'] = globStd / math.sqrt(len(feedbacks))
|
|
||||||
node['score'] = node['mean'] + errorFac*node['se']
|
node['score'] = node['mean'] + errorFac*node['se']
|
||||||
else:
|
else:
|
||||||
node['score'] = globMu + errorFac*globStd + len(feedbacks)*0.0000000001
|
node['score'] = globMu + errorFac*globStd + len(feedbacks)*0.0000000001
|
||||||
@ -507,9 +509,9 @@ def addScoreToLabels(G):
|
|||||||
node['label'] += " ("+str(node['rating'])+")"
|
node['label'] += " ("+str(node['rating'])+")"
|
||||||
else:
|
else:
|
||||||
if 'score' in node and node['score'] != None:
|
if 'score' in node and node['score'] != None:
|
||||||
node['label'] += " (~{:.2f}".format(node['score'])+")"
|
node['label'] += " (~{:.2f}±{:.2f})".format(node['score'], node['std'])
|
||||||
else:
|
else:
|
||||||
node['label'] += " (~0)"
|
node['label'] += " (~0±∞)"
|
||||||
|
|
||||||
|
|
||||||
def genAndShowHTML(G, showButtons=False):
|
def genAndShowHTML(G, showButtons=False):
|
||||||
@ -674,8 +676,14 @@ def waveFlow(G, node, n, dist, menge, firstEdge=False):
|
|||||||
if node in bestlist or node in keeplist:
|
if node in bestlist or node in keeplist:
|
||||||
waveFlow(G, node, m, dist, menge, firstEdge=firstEdge)
|
waveFlow(G, node, m, dist, menge, firstEdge=firstEdge)
|
||||||
|
|
||||||
def evaluateFitness():
|
def evaluateFitness(books):
|
||||||
G, books = buildFullGraph()
|
G = buildBookGraph(books)
|
||||||
|
graphAddAuthors(G, books)
|
||||||
|
graphAddRecommenders(G, books)
|
||||||
|
graphAddTopLists(G, books)
|
||||||
|
graphAddSeries(G, books)
|
||||||
|
graphAddTags(G, books)
|
||||||
|
|
||||||
ratedBooks = [n for n in list(G.nodes) if 'rating' in G.nodes[n] and G.nodes[n]['rating'] != None]
|
ratedBooks = [n for n in list(G.nodes) if 'rating' in G.nodes[n] and G.nodes[n]['rating'] != None]
|
||||||
errSq = []
|
errSq = []
|
||||||
for m in ratedBooks:
|
for m in ratedBooks:
|
||||||
@ -691,26 +699,28 @@ def evaluateFitness():
|
|||||||
|
|
||||||
def train(gamma = 0.1):
|
def train(gamma = 0.1):
|
||||||
global weights
|
global weights
|
||||||
|
books = loadBooksFromDB()
|
||||||
bestWeights = copy.copy(weights)
|
bestWeights = copy.copy(weights)
|
||||||
best_mse = evaluateFitness()
|
best_mse = evaluateFitness(books)
|
||||||
w = list(weights.keys())
|
w = list(weights.keys())
|
||||||
attr = random.choice(w)
|
attr = random.choice(w)
|
||||||
delta = gamma * (-0.5 + (0.75 + 0.25*random.random()))
|
delta = gamma * (-0.5 + (0.75 + 0.25*random.random()))
|
||||||
|
|
||||||
while True:
|
while gamma > 1.0e-08:
|
||||||
print({'mse': best_mse, 'w': weights, 'gamma': gamma})
|
print({'mse': best_mse, 'w': weights, 'gamma': gamma})
|
||||||
weights = copy.copy(bestWeights)
|
weights = copy.copy(bestWeights)
|
||||||
if gamma < 0.01 and random.random() < 0.5:
|
if gamma < 0.01:
|
||||||
gamma = 0.01
|
while random.random() < 0.5:
|
||||||
weights[attr] = -1+random.random()*2
|
attr = random.choice(w)
|
||||||
|
weights[attr] = -0.1+random.random()*1.5
|
||||||
else:
|
else:
|
||||||
weights[attr] += delta
|
weights[attr] += delta
|
||||||
if attr not in ['sigma, mu']:
|
if attr not in ['sigma', 'mu', 'stability']:
|
||||||
weights[attr] = min(max(0, weight[attr]), 1.5)
|
weights[attr] = min(max(0, weights[attr]), 3)
|
||||||
mse = evaluateFitness()
|
mse = evaluateFitness(books)
|
||||||
if mse < best_mse: # got better
|
if mse < best_mse: # got better
|
||||||
saveWeights(weights)
|
saveWeights(weights)
|
||||||
gamma *= 1.75
|
gamma = max(gamma*1.75, 0.001)
|
||||||
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