我编写了以下代码:
class PredictionSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Prediction
fields = ["id", "market"]
depth = 1
class FixtureSerializer(serializers.HyperlinkedModelSerializer):
predictions = PredictionSerializer()
class Meta:
model = Fixture
fields = ["sofascore_id", "home", "away", "league", "round", "date", "predictions"]
depth = 1
class FixtureViewSet(viewsets.ReadOnlyModelViewSet):
"""
API endpoint that allows fixture to be viewed or edited.
"""
serializer_class = FixtureSerializer
permission_classes = [permissions.IsAuthenticated]
filter_backends = [django_filters.rest_framework.DjangoFilterBackend]
filterset_fields = ("home", "away", "sofascore_id", "statistics")
def get_queryset(self):
date = self.request.query_params.get('date')
queryset = Fixture.objects.all().order_by('-date')
if(date):
queryset = Fixture.objects.filter(date__date=date).order_by('date')
return queryset
对应于以下模型:
class Market(models.Model):
name = models.CharField(max_length=200)
class Fixture(models.Model):
sofascore_id = models.CharField(max_length=200)
home = models.ForeignKey(Team, on_delete=models.CASCADE, related_name="home")
away = models.ForeignKey(Team, on_delete=models.CASCADE, related_name="away")
league = models.ForeignKey(League, on_delete=models.CASCADE, blank=True)
round = models.CharField(max_length=200, default=None, blank=True, null=True)
date = models.DateTimeField()
statistics = models.ForeignKey(Statistics, on_delete=models.CASCADE, default=None, blank=True, null=True)
updated_at = models.DateTimeField(auto_now=True)
created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return u'{0} - {1}'.format(self.home.name, self.away.name)
class Prediction(models.Model):
market = models.ForeignKey(Market, on_delete=models.CASCADE, blank=True)
fixture = models.ForeignKey(to=Fixture, on_delete=models.CASCADE, related_name="predictions", null=True, blank=True)
当我向/ fixtures请求时,“预测”字典始终为空,但是当我使用以下语句运行一些测试代码时:
f = Fixture.objects.get(sofascore_id=match).predictions.all()
我得到以下作为返回值:
<QuerySet [<Prediction: Prediction object (23)>, <Prediction: Prediction object (24)>, <Prediction: Prediction object (25)>, <Prediction: Prediction object (26)>, <Prediction: Prediction object (27)>, <Prediction: Prediction object (28)>]>
这是什么问题,为什么我无法使用书面代码来检索我的预测?
答案 0 :(得分:1)
您需要在序列化程序中指定属性def build_regressor(timesteps,units,optimizer):
X_train = []
y_train = []
for i in range(timesteps, np.ma.size(training_set_scaled)):
X_train.append(training_set_scaled[i-timesteps:i, 0])
y_train.append(training_set_scaled[i, 0])
X_train, y_train = np.array(X_train), np.array(y_train)
X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))
regressor = Sequential()
regressor.add(LSTM(units = units, return_sequences = True, input_shape = (X_train.shape[1], X_train.shape[2])))
regressor.add(Dropout(rate=0.2))
regressor.add(LSTM(units = units, return_sequences = True))
regressor.add(Dropout(rate=0.2))
regressor.add(LSTM(units = units, return_sequences = True))
regressor.add(Dropout(rate=0.2))
regressor.add(LSTM(units = units))
regressor.add(Dropout(rate=0.2))
regressor.add(Dense(units = 1))
regressor.compile(optimizer = optimizer, loss = 'mean_squared_error')
return regressor, X_train, y_train
regressor = KerasRegressor(build_fn = build_regressor)
parameters = {'batch_size': [25, 32],
'epochs': [100, 200],
'optimizer': ['adam', 'rmsprop'],
'units': [50,75],
'timesteps': [60,120]}
grid_search = GridSearchCV(estimator = regressor,
param_grid = parameters,
scoring = 'neg_mean_squared_error',
cv = 10)
grid_search = grid_search.fit(X_train, y_train)
best_parameters = grid_search.best_params_
best_accuracy = grid_search.best_score_
:
many