You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

107 line
2.9 KiB

  1. import torch
  2. import dashscope
  3. from http import HTTPStatus
  4. from dashscope import TextEmbedding
  5. from torch import nn
  6. import torch.optim as optim
  7. import pandas as pd
  8. import numpy as np
  9. import models
  10. dashscope.api_key = 'sk-44ccc9ab5e754eddb545cade12b632cf'
  11. cache = {}
  12. answerCache = []
  13. def getem(question):
  14. global cache
  15. if question in cache.keys():
  16. return cache[question]
  17. resp = TextEmbedding.call(model=TextEmbedding.Models.text_embedding_v1,
  18. input=question,
  19. text_type='query')
  20. if resp.status_code == HTTPStatus.OK:
  21. cache[question] = resp['output']['embeddings'][0]['embedding']
  22. return resp['output']['embeddings'][0]['embedding']
  23. model = models.cnn.CNN()
  24. criterion = nn.MSELoss()
  25. optimizer = optim.Adam(model.parameters(), lr=0.001)
  26. train = []
  27. test = []
  28. dataall = pd.read_csv('data/data.csv')
  29. dataall = dataall.iloc[:, 1:4]
  30. dataall = dataall.sample(frac=1)
  31. train = dataall.iloc[0:300]
  32. train = train.reset_index(drop=True)
  33. test = dataall.iloc[300:]
  34. test = test.reset_index(drop=True)
  35. nlossLast = 0
  36. for i in range(1):
  37. nloss = 0
  38. for k in range(len(train)):
  39. va = getem(train.iloc[k]['question'])
  40. vb = getem(train.iloc[k]['answer'])
  41. if train.iloc[k]['answer'] not in answerCache:
  42. answerCache.append(train.iloc[k]['answer'])
  43. trainTensor = torch.Tensor([va, vb]).reshape([1, 1, len(va) + len(vb)])
  44. output = model(trainTensor)
  45. # 计算损失
  46. if train.iloc[k]['label'] == 1:
  47. loss = criterion(output,
  48. torch.tensor([1, 0]).float().reshape([1, 2]))
  49. else:
  50. loss = criterion(output,
  51. torch.tensor([0, 1]).float().reshape([1, 2]))
  52. # 反向传播并更新权重
  53. optimizer.zero_grad()
  54. loss.backward()
  55. optimizer.step()
  56. nloss += loss
  57. if k % 50 == 0:
  58. print(i, k, 'done')
  59. if abs(nloss - nlossLast) < 0.0001:
  60. continue
  61. else:
  62. nlossLast = nloss
  63. print('one loop done', nloss/len(train))
  64. p = 0
  65. for i in range(len(test)):
  66. va = getem(test.iloc[i]['question'])
  67. Scores = np.zeros(len(answerCache))
  68. for j in range(len(answerCache)):
  69. vb = getem(answerCache[j])
  70. testTensor = torch.Tensor([va, vb]).reshape([1, 1, 3072])
  71. output = model(testTensor)
  72. Scores[j] = output[0][0]
  73. for k in range(2):
  74. if test.iloc[i]['label'] == 1:
  75. vc = test.iloc[i]['answer']
  76. else:
  77. vc = ''
  78. tt = Scores.argmax()
  79. if Scores[tt] > 0.5:
  80. vb = answerCache[tt]
  81. Scores[tt] = -1
  82. else:
  83. vb = ''
  84. if vb == vc:
  85. p += 1
  86. break
  87. print(p/len(test))
  88. def main(**param):
  89. print('this message is from main function')
  90. if __name__ == '__main__':
  91. main()
  92. print('now __name__ is %s' % __name__)